Struct join_string::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, I::Item: Display,

source

pub fn into_string(self) -> String

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 7)
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
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 + 1u32).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());
}
source

pub fn write_fmt<W: Write>(self, writer: W) -> Result

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<()>

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)
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(())
}
More examples
Hide additional examples
examples/simple.rs (line 9)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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(())
}

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 + Debug, S: Display + 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 Stringwhere 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> RefUnwindSafe for Joiner<I, S>where I: RefUnwindSafe, S: RefUnwindSafe,

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

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 Twhere U: TryFrom<T>,

§

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.