Skip to content

Commit

Permalink
Add try_collect method to Itertools
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Jan 8, 2020
1 parent 505e0f4 commit 3301588
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/lib.rs
Expand Up @@ -1541,6 +1541,35 @@ pub trait Itertools : Iterator {
self.collect()
}

/// `.try_collect()` is more convenient way of writing
/// `.collect::<Result<_, _>>()`
///
/// # Example
///
/// ```
/// use std::{fs, io};
/// use itertools::Itertools;
///
/// fn process_dir_entries(entries: &[fs::DirEntry]) {
/// // ...
/// }
///
/// fn main() -> std::io::Result<()> {
/// let entries: Vec<_> = fs::read_dir(".")?.try_collect()?;
/// process_dir_entries(&entries);
///
/// Ok(())
/// }
/// ```
#[cfg(feature = "use_std")]
fn try_collect<T, U, E>(self) -> Result<U, E>
where
Self: Sized + Iterator<Item = Result<T, E>>,
Result<U, E>: FromIterator<Result<T, E>>,
{
self.collect()
}

/// Assign to each reference in `self` from the `from` iterator,
/// stopping at the shortest of the two iterators.
///
Expand Down

0 comments on commit 3301588

Please sign in to comment.