Crate string_join[][src]

String join

Uses a python-like syntax to join any iterator-like thing with any string-like thing


use string_join::Join;

assert_eq!("=".join(&["a", "b", "c"]), String::from("a=b=c"));

You can also write a joined collection directly to an io::Writer

use std::fs::File;
use string_join::display::Join;

let mut f = File::create("foo.txt")?;
"\n".write_join(&mut f, &['a', 'b', 'c'])?; // => writes `a\nb\nc` to the writer and returns
                                            //    the number of bytes written (5, in this case)

AsRef<str> vs Display

There are two ways to convert items in a collection to a joined string. If all the items, as well as the separator, implement AsRef<str>, then we only have to allocate a single string and can write bytes into it. However, this limits the types that we can join. If we instead limit the types we can join to types that implement Display, we can join a lot more types, with the tradeoff that a lot more allocations will be happening.

Therefore, there are two modules in this crate, string_join::as_ref and string_join::display. They both provide a trait called Join and both the traits have methods write_join and join. However, one is implemented for AsRef<str> types and one for Display types. This way, the user of the crate can decide which implementation they want to use.

To keep backwards compatibility, string_join::Join is the same as string_join::as_ref::Join.


pub use crate::as_ref::Join;



A Join implementation for iterator item types that implement AsRef<str>


A Join implementation for iterator item types that implement Display