Skip to content

Commit 0bb818c

Browse files
committed
Add Iterator::try_for_each
The fallible version of for_each and the stateless version of try_fold.
1 parent afa8acc commit 0bb818c

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/libcore/iter/iterator.rs

+42-6
Original file line numberDiff line numberDiff line change
@@ -1366,9 +1366,9 @@ pub trait Iterator {
13661366
///
13671367
/// In particular, try to have this call `try_fold()` on the internal parts
13681368
/// 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
13721372
/// resumable after hitting an error here.
13731373
///
13741374
/// # Examples
@@ -1414,6 +1414,42 @@ pub trait Iterator {
14141414
Try::from_ok(accum)
14151415
}
14161416

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+
14171453
/// An iterator method that applies a function, producing a single, final value.
14181454
///
14191455
/// `fold()` takes two arguments: an initial value, and a closure with two
@@ -1528,7 +1564,7 @@ pub trait Iterator {
15281564
fn all<F>(&mut self, mut f: F) -> bool where
15291565
Self: Sized, F: FnMut(Self::Item) -> bool
15301566
{
1531-
self.try_fold((), move |(), x| {
1567+
self.try_for_each(move |x| {
15321568
if f(x) { LoopState::Continue(()) }
15331569
else { LoopState::Break(()) }
15341570
}) == LoopState::Continue(())
@@ -1577,7 +1613,7 @@ pub trait Iterator {
15771613
Self: Sized,
15781614
F: FnMut(Self::Item) -> bool
15791615
{
1580-
self.try_fold((), move |(), x| {
1616+
self.try_for_each(move |x| {
15811617
if f(x) { LoopState::Break(()) }
15821618
else { LoopState::Continue(()) }
15831619
}) == LoopState::Break(())
@@ -1631,7 +1667,7 @@ pub trait Iterator {
16311667
Self: Sized,
16321668
P: FnMut(&Self::Item) -> bool,
16331669
{
1634-
self.try_fold((), move |(), x| {
1670+
self.try_for_each(move |x| {
16351671
if predicate(&x) { LoopState::Break(x) }
16361672
else { LoopState::Continue(()) }
16371673
}).break_value()

0 commit comments

Comments
 (0)