@@ -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
@@ -1532,7 +1568,7 @@ pub trait Iterator {
1532
1568
fn all < F > ( & mut self , mut f : F ) -> bool where
1533
1569
Self : Sized , F : FnMut ( Self :: Item ) -> bool
1534
1570
{
1535
- self . try_fold ( ( ) , move |( ) , x| {
1571
+ self . try_for_each ( move |x| {
1536
1572
if f ( x) { LoopState :: Continue ( ( ) ) }
1537
1573
else { LoopState :: Break ( ( ) ) }
1538
1574
} ) == LoopState :: Continue ( ( ) )
@@ -1581,7 +1617,7 @@ pub trait Iterator {
1581
1617
Self : Sized ,
1582
1618
F : FnMut ( Self :: Item ) -> bool
1583
1619
{
1584
- self . try_fold ( ( ) , move |( ) , x| {
1620
+ self . try_for_each ( move |x| {
1585
1621
if f ( x) { LoopState :: Break ( ( ) ) }
1586
1622
else { LoopState :: Continue ( ( ) ) }
1587
1623
} ) == LoopState :: Break ( ( ) )
@@ -1635,7 +1671,7 @@ pub trait Iterator {
1635
1671
Self : Sized ,
1636
1672
P : FnMut ( & Self :: Item ) -> bool ,
1637
1673
{
1638
- self . try_fold ( ( ) , move |( ) , x| {
1674
+ self . try_for_each ( move |x| {
1639
1675
if predicate ( & x) { LoopState :: Break ( x) }
1640
1676
else { LoopState :: Continue ( ( ) ) }
1641
1677
} ) . break_value ( )
0 commit comments