std-method

DoubleEndedIterator::try_rfold — checked reverse fold

Accumulate from the back and stop on failure. On a normal double-ended array iterator, the failing item is consumed from the back, leaving the earlier items available. Expected stdout (not executed): None Some(7) Common mistake: Assuming the remaining items are those after the failing item in forward order.

Formal Syntax & Structure

rust syntaxrust
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R where Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Output = B>

Working Example

Example Snippetrust
fn main() {    let mut amounts = [7_u8, 250, 10].into_iter();    let total = amounts.try_rfold(0_u8, |acc, n| acc.checked_add(n));    println!("{total:?} {:?}", amounts.next());}
Rust Reference