pub struct Joiner<I, S>{ /* private fields */ }
Expand description
Helper struct that captures the iterator and separator for later joining.
Implementations§
Source§impl<I, S> Joiner<I, S>
impl<I, S> Joiner<I, S>
Sourcepub fn new(iter: I, sep: S) -> Self
pub fn new(iter: I, sep: S) -> Self
Create a Joiner
object.
You can use this when implementing your own join()
function.
Sourcepub fn into_string(self) -> String
pub fn into_string(self) -> 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}
Sourcepub fn write_fmt<W: Write>(self, writer: W) -> Result
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
.
Sourcepub fn write_io<W: Write>(self, writer: W) -> Result<()>
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)
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
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§
Auto Trait Implementations§
impl<I, S> Freeze for Joiner<I, S>
impl<I, S> RefUnwindSafe for Joiner<I, S>where
I: RefUnwindSafe,
S: RefUnwindSafe,
impl<I, S> Send for Joiner<I, S>
impl<I, S> Sync for Joiner<I, S>
impl<I, S> Unpin for Joiner<I, S>
impl<I, S> UnwindSafe for Joiner<I, S>where
I: UnwindSafe,
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more