@@ -1366,9 +1366,9 @@ pub trait Iterator {
1366
1366
///
1367
1367
/// In particular, try to have this call `try_fold()` on the internal parts
1368
1368
/// from which this iterator is composed. If multiple calls are needed,
1369
- /// the `?` operator be convenient for chaining the accumulator value along,
1370
- /// but beware any invariants that need to be upheld before those early
1371
- /// returns. This is a `&mut self` method, so iteration needs to be
1369
+ /// the `?` operator may be convenient for chaining the accumulator value
1370
+ /// along, but beware any invariants that need to be upheld before those
1371
+ /// early returns. This is a `&mut self` method, so iteration needs to be
1372
1372
/// resumable after hitting an error here.
1373
1373
///
1374
1374
/// # Examples
@@ -1414,6 +1414,42 @@ pub trait Iterator {
1414
1414
Try :: from_ok ( accum)
1415
1415
}
1416
1416
1417
+ /// An iterator method that applies a fallible function to each item in the
1418
+ /// iterator, stopping at the first error and returning that error.
1419
+ ///
1420
+ /// This can also be thought of as the fallible form of [`for_each()`]
1421
+ /// or as the stateless version of [`try_fold()`].
1422
+ ///
1423
+ /// [`for_each()`]: #method.for_each
1424
+ /// [`try_fold()`]: #method.try_fold
1425
+ ///
1426
+ /// # Examples
1427
+ ///
1428
+ /// ```
1429
+ /// #![feature(iterator_try_fold)]
1430
+ /// use std::fs::rename;
1431
+ /// use std::io::{stdout, Write};
1432
+ /// use std::path::Path;
1433
+ ///
1434
+ /// let data = ["no_tea.txt", "stale_bread.json", "torrential_rain.png"];
1435
+ ///
1436
+ /// let res = data.iter().try_for_each(|x| writeln!(stdout(), "{}", x));
1437
+ /// assert!(res.is_ok());
1438
+ ///
1439
+ /// let mut it = data.iter().cloned();
1440
+ /// let res = it.try_for_each(|x| rename(x, Path::new(x).with_extension("old")));
1441
+ /// assert!(res.is_err());
1442
+ /// // It short-circuited, so the remaining items are still in the iterator:
1443
+ /// assert_eq!(it.next(), Some("stale_bread.json"));
1444
+ /// ```
1445
+ #[ inline]
1446
+ #[ unstable( feature = "iterator_try_fold" , issue = "45594" ) ]
1447
+ fn try_for_each < F , R > ( & mut self , mut f : F ) -> R where
1448
+ Self : Sized , F : FnMut ( Self :: Item ) -> R , R : Try < Ok =( ) >
1449
+ {
1450
+ self . try_fold ( ( ) , move |( ) , x| f ( x) )
1451
+ }
1452
+
1417
1453
/// An iterator method that applies a function, producing a single, final value.
1418
1454
///
1419
1455
/// `fold()` takes two arguments: an initial value, and a closure with two
@@ -1528,7 +1564,7 @@ pub trait Iterator {
1528
1564
fn all < F > ( & mut self , mut f : F ) -> bool where
1529
1565
Self : Sized , F : FnMut ( Self :: Item ) -> bool
1530
1566
{
1531
- self . try_fold ( ( ) , move |( ) , x| {
1567
+ self . try_for_each ( move |x| {
1532
1568
if f ( x) { LoopState :: Continue ( ( ) ) }
1533
1569
else { LoopState :: Break ( ( ) ) }
1534
1570
} ) == LoopState :: Continue ( ( ) )
@@ -1577,7 +1613,7 @@ pub trait Iterator {
1577
1613
Self : Sized ,
1578
1614
F : FnMut ( Self :: Item ) -> bool
1579
1615
{
1580
- self . try_fold ( ( ) , move |( ) , x| {
1616
+ self . try_for_each ( move |x| {
1581
1617
if f ( x) { LoopState :: Break ( ( ) ) }
1582
1618
else { LoopState :: Continue ( ( ) ) }
1583
1619
} ) == LoopState :: Break ( ( ) )
@@ -1631,7 +1667,7 @@ pub trait Iterator {
1631
1667
Self : Sized ,
1632
1668
P : FnMut ( & Self :: Item ) -> bool ,
1633
1669
{
1634
- self . try_fold ( ( ) , move |( ) , x| {
1670
+ self . try_for_each ( move |x| {
1635
1671
if predicate ( & x) { LoopState :: Break ( x) }
1636
1672
else { LoopState :: Continue ( ( ) ) }
1637
1673
} ) . break_value ( )
0 commit comments