File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -1541,6 +1541,35 @@ pub trait Itertools : Iterator {
1541
1541
self . collect ( )
1542
1542
}
1543
1543
1544
+ /// `.try_collect()` is more convenient way of writing
1545
+ /// `.collect::<Result<_, _>>()`
1546
+ ///
1547
+ /// # Example
1548
+ ///
1549
+ /// ```
1550
+ /// use std::{fs, io};
1551
+ /// use itertools::Itertools;
1552
+ ///
1553
+ /// fn process_dir_entries(entries: &[fs::DirEntry]) {
1554
+ /// // ...
1555
+ /// }
1556
+ ///
1557
+ /// fn do_stuff() -> std::io::Result<()> {
1558
+ /// let entries: Vec<_> = fs::read_dir(".")?.try_collect()?;
1559
+ /// process_dir_entries(&entries);
1560
+ ///
1561
+ /// Ok(())
1562
+ /// }
1563
+ /// ```
1564
+ #[ cfg( feature = "use_std" ) ]
1565
+ fn try_collect < T , U , E > ( self ) -> Result < U , E >
1566
+ where
1567
+ Self : Sized + Iterator < Item = Result < T , E > > ,
1568
+ Result < U , E > : FromIterator < Result < T , E > > ,
1569
+ {
1570
+ self . collect ( )
1571
+ }
1572
+
1544
1573
/// Assign to each reference in `self` from the `from` iterator,
1545
1574
/// stopping at the shortest of the two iterators.
1546
1575
///
You can’t perform that action at this time.
0 commit comments