Skip to content

Commit bd35161

Browse files
committed
try_rfold_last
1 parent 249db11 commit bd35161

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

library/core/src/iter/traits/double_ended.rs

+27
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,33 @@ pub trait DoubleEndedIterator: Iterator {
339339
Some(self.rfold(last, folding))
340340
}
341341

342+
/// This is the reverse version of [`Iterator::try_fold_first()`]: it takes
343+
/// elements starting from the back of the iterator.
344+
#[inline]
345+
#[unstable(feature = "iterator_try_rfold_last", reason = "new API", issue = "none")]
346+
fn try_rfold_last<F1, FR, R>(
347+
&mut self,
348+
init: F1,
349+
folding: FR,
350+
) -> ChangeOutputType<R, Option<R::Output>>
351+
where
352+
Self: Sized,
353+
F1: FnOnce(Self::Item) -> R,
354+
FR: FnMut(R::Output, Self::Item) -> R,
355+
R: Try,
356+
R::Residual: Residual<Option<R::Output>>,
357+
{
358+
let last = match self.next_back() {
359+
Some(i) => init(i)?,
360+
None => return Try::from_output(None),
361+
};
362+
363+
match self.try_rfold(last, folding).branch() {
364+
ControlFlow::Break(r) => FromResidual::from_residual(r),
365+
ControlFlow::Continue(i) => Try::from_output(Some(i)),
366+
}
367+
}
368+
342369
/// Searches for an element of an iterator from the back that satisfies a predicate.
343370
///
344371
/// `rfind()` takes a closure that returns `true` or `false`. It applies

0 commit comments

Comments
 (0)