Last active
May 3, 2022 05:47
-
-
Save JosephTLyons/ff4cf0ba22a7ff4c5a84fb1b316a1db8 to your computer and use it in GitHub Desktop.
A generic trait that converts iterables into a `String`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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