Struct Joiner

Source
pub struct Joiner<I, S>
where I: Iterator, S: Display,
{ /* private fields */ }
Expand description

Helper struct that captures the iterator and separator for later joining.

Implementations§

Source§

impl<I, S> Joiner<I, S>
where I: Iterator, S: Display,

Source

pub fn new(iter: I, sep: S) -> Self

Create a Joiner object.

You can use this when implementing your own join() function.

Source

pub fn into_string(self) -> String
where I::Item: Display,

Consumes the backing iterator of a Joiner and returns the joined elements as a new String.

Examples found in repository?
examples/reverse_words.rs (line 8)
3pub fn reverse_words(s: impl AsRef<str>) -> String {
4    s.as_ref()
5        .split_whitespace()
6        .map(|s| s.chars().rev().join(""))
7        .join(" ")
8        .into_string()
9}
10
11fn main() {
12    println!("{}", reverse_words("foo bar baz"));
13
14    println!(
15        "{}",
16        "foo bar baz"
17            .split_whitespace()
18            .map(|s| s.chars().rev().join(""))
19            .join(" ")
20    );
21
22    println!(
23        "{}",
24        "foo bar baz"
25            .split_whitespace()
26            .map(|s| s
27                .chars()
28                .rev()
29                .map(|c| char::from_u32(c as u32 + 1).unwrap_or('?'))
30                .join(""))
31            .join(" ")
32    );
33
34    // inefficient temporary strings
35    println!(
36        "{}",
37        "foo bar baz"
38            .split_whitespace()
39            .map(|s| s.chars().rev().map(|c| format!("<{c}>")).join(""))
40            .join(" ")
41    );
42
43    // inefficient temporary strings
44    println!(
45        "{}",
46        std::env::args()
47            .map(|s| s.chars().rev().collect::<String>())
48            .join(" ")
49            .into_string()
50    );
51}
Source

pub fn write_fmt<W: Write>(self, writer: W) -> Result
where I::Item: Display,

Consumes the backing iterator of a Joiner and writes the joined elements into a std::fmt::Write.

Source

pub fn write_io<W: Write>(self, writer: W) -> Result<()>
where I::Item: Display,

Consumes the backing iterator of a Joiner and writes the joined elements into a std::io::Write.

Examples found in repository?
examples/file.rs (line 14)
6fn main() -> std::io::Result<()> {
7    let mut args = std::env::args().skip(1);
8
9    let filename = args.next().expect(USAGE);
10    let sep = args.next().expect(USAGE);
11
12    let file = File::create(filename)?;
13
14    args.join(sep).write_io(file)?;
15
16    Ok(())
17}
More examples
Hide additional examples
examples/simple.rs (line 12)
3fn main() -> std::io::Result<()> {
4    println!("{}", ["foo", "bar", "baz"].join(", "));
5    println!("{}", ['a', 'b', 'c'].join(", "));
6    println!(
7        "{}",
8        ["foo".to_owned(), "bar".to_owned(), "baz".to_owned()].join(", ")
9    );
10    println!("{}", vec![1, 2, 3].iter().cycle().take(5).join(", "));
11    println!("{}", "äüö".chars().join(' '));
12    std::env::args().join(", ").write_io(std::io::stdout())?;
13    println!();
14
15    // inefficient temporary string
16    let str: String = std::env::args().join(", ").into();
17    println!("{}", str);
18
19    Ok(())
20}

Trait Implementations§

Source§

impl<I, S> Clone for Joiner<I, S>
where I: Iterator + Clone, S: Display + Clone, I::Item: Display,

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<I, S> Debug for Joiner<I, S>
where I: Iterator + Clone, S: Display, I::Item: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I, S> Display for Joiner<I, S>
where I: Iterator + Clone, S: Display, I::Item: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I, S> From<Joiner<I, S>> for String
where I: Iterator, S: Display, I::Item: Display,

Source§

fn from(value: Joiner<I, S>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<I, S> Freeze for Joiner<I, S>
where I: Freeze, S: Freeze,

§

impl<I, S> RefUnwindSafe for Joiner<I, S>

§

impl<I, S> Send for Joiner<I, S>
where I: Send, S: Send,

§

impl<I, S> Sync for Joiner<I, S>
where I: Sync, S: Sync,

§

impl<I, S> Unpin for Joiner<I, S>
where I: Unpin, S: Unpin,

§

impl<I, S> UnwindSafe for Joiner<I, S>
where I: UnwindSafe, S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.