pub trait IteratorExtensions: Iterator + Sized {
fn try_map<U, E, F>(self, f: F) -> Result<impl Iterator<Item = U>, E>
where
F: FnMut(Self::Item) -> Result<U, E>,
{
Ok(self.map(f).collect::<Result<Vec<U>, E>>()?.into_iter())
}
fn to_vec(self) -> Vec<Self::Item> {
self.collect()
}
fn try_to_vec<T, E>(self) -> Result<Vec<T>, E>
where
Self: Iterator<Item = Result<T, E>>,
{
self.collect()
}
}
impl<I: Iterator + Sized> IteratorExtensions for I {}
pub trait IntoIteratorExtensions: Iterator + Sized {
fn join_to_string(self, sep: impl AsRef<str>) -> String
where
Self::Item: ToString,
{
self.into_iter()
.map(|t| t.to_string())
.to_vec()
.join(sep.as_ref())
}
}
impl<I: Iterator + Sized> IntoIteratorExtensions for I {}