@@ -301,6 +301,44 @@ pub trait DoubleEndedIterator: Iterator {
301
301
accum
302
302
}
303
303
304
+ /// Folds every element into an accumulator by applying an operation,
305
+ /// returning the final result, starting from the back.
306
+ /// The initial value is derived from the last element using the provided method.
307
+ ///
308
+ /// This is the reverse version of [`Iterator::fold_first()`]: it takes elements
309
+ /// starting from the back of the iterator.
310
+ ///
311
+ /// If the iterator is empty, returns [`None`]; otherwise, returns the
312
+ /// result of the fold.
313
+ ///
314
+ /// The folding function is a closure with two arguments: an 'accumulator', and an element.
315
+ ///
316
+ /// # Example
317
+ ///
318
+ /// ```
319
+ /// #![feature(iterator_rfold_last)]
320
+ ///
321
+ /// let numbers = [1, 2, 3, 4, 5];
322
+ ///
323
+ /// let result = numbers.iter().rfold_last(
324
+ /// |last| last.to_string(),
325
+ /// |acc, &x| format!("({x} + {acc})"),
326
+ /// ).unwrap();
327
+ ///
328
+ /// assert_eq!(result, "(1 + (2 + (3 + (4 + 5))))");
329
+ /// ```
330
+ #[ inline]
331
+ #[ unstable( feature = "iterator_rfold_last" , reason = "new API" , issue = "none" ) ]
332
+ fn rfold_last < B , F1 , FR > ( mut self , init : F1 , folding : FR ) -> Option < B >
333
+ where
334
+ Self : Sized ,
335
+ F1 : FnOnce ( Self :: Item ) -> B ,
336
+ FR : FnMut ( B , Self :: Item ) -> B ,
337
+ {
338
+ let last = init ( self . next_back ( ) ?) ;
339
+ Some ( self . rfold ( last, folding) )
340
+ }
341
+
304
342
/// Searches for an element of an iterator from the back that satisfies a predicate.
305
343
///
306
344
/// `rfind()` takes a closure that returns `true` or `false`. It applies
0 commit comments