add iter_join helper method to more efficiently join string vecs

This commit is contained in:
Edward Rudd 2022-09-26 12:24:48 -04:00
parent 3a6540752c
commit 580b972f45

31
src/utils.rs Normal file
View 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
}