Skip to content

Instantly share code, notes, and snippets.

@JosephTLyons
Last active May 3, 2022 05:47
Show Gist options
  • Save JosephTLyons/ff4cf0ba22a7ff4c5a84fb1b316a1db8 to your computer and use it in GitHub Desktop.
Save JosephTLyons/ff4cf0ba22a7ff4c5a84fb1b316a1db8 to your computer and use it in GitHub Desktop.
A generic trait that converts iterables into a `String`
trait IterToString {
fn iter_to_string(self, separator: &str) -> String;
}
impl<T> IterToString for T
where
T: IntoIterator,
T::Item: ToString,
{
fn iter_to_string(self, separator: &str) -> String {
self.into_iter()
.map(|item| item.to_string())
.collect::<Vec<_>>()
.join(separator)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment