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,
impl<I, S> Joiner<I, S>where I: Iterator, S: Display, I::Item: Display,
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 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());
}
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)
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
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,
impl<I, S> Clone for Joiner<I, S>where I: Iterator + Clone, S: Display + Clone, I::Item: Display,
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> 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