Trait join_string::Join

source ·
pub trait Join<I: Iterator>: IntoIterator<IntoIter = I> {
    // Provided methods
    fn join<S>(self, sep: S) -> Joiner<I, S>
       where Self: Sized,
             S: Display,
             I::Item: Display { ... }
    fn join_str<S>(self, sep: S) -> Joiner<DisplayIter<I>, DisplayWrapper<S>>
       where Self: Sized,
             S: AsRef<str>,
             I::Item: AsRef<str> { ... }
}
Expand description

Trait that provides a method to join elements of an iterator, interspersing a separator between all elements.

This trait is implemented for anything that implements std::iter::IntoIterator, which is e.g. arrays, slices, Vec, and more.

Provided Methods§

source

fn join<S>(self, sep: S) -> Joiner<I, S>
where Self: Sized, S: Display, I::Item: Display,

Join the elements of an iterator, interspersing a separator between all elements.

The elements and the separator need to implement std::fmt::Display.

Examples found in repository?
examples/reverse_words.rs (line 6)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
pub fn reverse_words(s: impl AsRef<str>) -> String {
    s.as_ref()
        .split_whitespace()
        .map(|s| s.chars().rev().join(""))
        .join(" ")
        .into_string()
}

fn main() {
    println!("{}", reverse_words("foo bar baz"));

    println!(
        "{}",
        "foo bar baz"
            .split_whitespace()
            .map(|s| s.chars().rev().join(""))
            .join(" ")
    );

    println!(
        "{}",
        "foo bar baz"
            .split_whitespace()
            .map(|s| s
                .chars()
                .rev()
                .map(|c| char::from_u32(c as u32 + 1).unwrap_or('?'))
                .join(""))
            .join(" ")
    );

    // inefficient temporary strings
    println!(
        "{}",
        "foo bar baz"
            .split_whitespace()
            .map(|s| s.chars().rev().map(|c| format!("<{c}>")).join(""))
            .join(" ")
    );

    // inefficient temporary strings
    println!(
        "{}",
        std::env::args()
            .map(|s| s.chars().rev().collect::<String>())
            .join(" ")
            .into_string()
    );
}
More examples
Hide additional examples
examples/file.rs (line 14)
6
7
8
9
10
11
12
13
14
15
16
17
fn main() -> std::io::Result<()> {
    let mut args = std::env::args().skip(1);

    let filename = args.next().expect(USAGE);
    let sep = args.next().expect(USAGE);

    let file = File::create(filename)?;

    args.join(sep).write_io(file)?;

    Ok(())
}
examples/simple.rs (line 4)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() -> std::io::Result<()> {
    println!("{}", ["foo", "bar", "baz"].join(", "));
    println!("{}", ['a', 'b', 'c'].join(", "));
    println!(
        "{}",
        ["foo".to_owned(), "bar".to_owned(), "baz".to_owned()].join(", ")
    );
    println!("{}", vec![1, 2, 3].iter().cycle().take(5).join(", "));
    println!("{}", "äüö".chars().join(' '));
    std::env::args().join(", ").write_io(std::io::stdout())?;
    println!();

    // inefficient temporary string
    let str: String = std::env::args().join(", ").into();
    println!("{}", str);

    Ok(())
}
source

fn join_str<S>(self, sep: S) -> Joiner<DisplayIter<I>, DisplayWrapper<S>>
where Self: Sized, S: AsRef<str>, I::Item: AsRef<str>,

Join the elements of an iterator, interspersing a separator between all elements.

The elements and the separator need to implement AsRef<str>.

Implementors§

source§

impl<T> Join<<T as IntoIterator>::IntoIter> for T
where T: IntoIterator,