Function join_string::join_str

source ·
pub fn join_str<I, S>(
    elements: impl Join<I>,
    sep: S
) -> Joiner<impl Iterator<Item = impl Display>, impl Display>where
    I: Iterator,
    I::Item: AsRef<str>,
    S: AsRef<str>,
Expand description

Join anything that implements Join, not just iterators when elements don’t implement std::fmt::Display, but implement AsRef<str> instead.

You can pass iterators, slices, and borrows of arrays and Vecs:

use join_string::join_str;
 
assert_eq!(
    join_str(&["foo", "bar", "baz"], ", ").into_string(),
    "foo, bar, baz"
);
 
assert_eq!(
    join_str([
        &"foo".to_owned(),
        &"bar".to_owned(),
        &"baz".to_owned()
    ].as_slice(), ", ").into_string(),
    "foo, bar, baz"
);
 
assert_eq!(
    join_str(&vec!["foo", "bar", "baz"], ", ").into_string(),
    "foo, bar, baz"
);
 
assert_eq!(
    join_str([
        "foo".to_owned(),
        "bar".to_owned(),
        "baz".to_owned()
    ].iter().rev(), ", ").into_string(),
    "baz, bar, foo"
);