Skip to content

Commit 5fd8d18

Browse files
authored
Rollup merge of rust-lang#48157 - scottmcm:try-for-each, r=dtolnay
Add Iterator::try_for_each The fallible version of `for_each` aka the stateless version of `try_fold`. Inspired by @cuviper's comment in rust-lang#45379 (comment) as a more direct and obvious solution than `.map(f).collect::<Result<(), _>>()`. Like `for_each`, no need for an `r` version thanks to overrides in `Rev`. `iterator_try_fold` tracking issue: rust-lang#45594
2 parents 1e67c13 + 0bb818c commit 5fd8d18

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
@@ -1532,7 +1568,7 @@ pub trait Iterator {
15321568
fn all<F>(&mut self, mut f: F) -> bool where
15331569
Self: Sized, F: FnMut(Self::Item) -> bool
15341570
{
1535-
self.try_fold((), move |(), x| {
1571+
self.try_for_each(move |x| {
15361572
if f(x) { LoopState::Continue(()) }
15371573
else { LoopState::Break(()) }
15381574
}) == LoopState::Continue(())
@@ -1581,7 +1617,7 @@ pub trait Iterator {
15811617
Self: Sized,
15821618
F: FnMut(Self::Item) -> bool
15831619
{
1584-
self.try_fold((), move |(), x| {
1620+
self.try_for_each(move |x| {
15851621
if f(x) { LoopState::Break(()) }
15861622
else { LoopState::Continue(()) }
15871623
}) == LoopState::Break(())
@@ -1635,7 +1671,7 @@ pub trait Iterator {
16351671
Self: Sized,
16361672
P: FnMut(&Self::Item) -> bool,
16371673
{
1638-
self.try_fold((), move |(), x| {
1674+
self.try_for_each(move |x| {
16391675
if predicate(&x) { LoopState::Break(x) }
16401676
else { LoopState::Continue(()) }
16411677
}).break_value()

0 commit comments

Comments
 (0)