add iter_join helper method to more efficiently join string vecs
This commit is contained in:
parent
3a6540752c
commit
580b972f45
1 changed files with 31 additions and 0 deletions
31
src/utils.rs
Normal file
31
src/utils.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/// Lovingly borrowed from the cargo crate
|
||||
///
|
||||
/// Joins an iterator of [std::fmt::Display]'ables into an output writable
|
||||
pub(crate) fn iter_join_onto<W, I, T>(mut w: W, iter: I, delim: &str) -> std::fmt::Result
|
||||
where
|
||||
W: std::fmt::Write,
|
||||
I: IntoIterator<Item = T>,
|
||||
T: std::fmt::Display,
|
||||
{
|
||||
let mut it = iter.into_iter().peekable();
|
||||
while let Some(n) = it.next() {
|
||||
write!(w, "{}", n)?;
|
||||
if it.peek().is_some() {
|
||||
write!(w, "{}", delim)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Lovingly borrowed from the cargo crate
|
||||
///
|
||||
/// Joins an iterator of [std::fmt::Display]'ables to a new [std::string::String].
|
||||
pub(crate) fn iter_join<I, T>(iter: I, delim: &str) -> String
|
||||
where
|
||||
I: IntoIterator<Item = T>,
|
||||
T: std::fmt::Display,
|
||||
{
|
||||
let mut s = String::new();
|
||||
let _ = iter_join_onto(&mut s, iter, delim);
|
||||
s
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue