@@ -2028,7 +2028,8 @@ pub trait Iterator {
20282028 self . try_fold ( ( ) , call ( f) )
20292029 }
20302030
2031- /// An iterator method that applies a function, producing a single, final value.
2031+ /// Folds every element into an accumulator by applying an operation,
2032+ /// returning the final result.
20322033 ///
20332034 /// `fold()` takes two arguments: an initial value, and a closure with two
20342035 /// arguments: an 'accumulator', and an element. The closure returns the value that
@@ -2049,6 +2050,9 @@ pub trait Iterator {
20492050 /// may not terminate for infinite iterators, even on traits for which a
20502051 /// result is determinable in finite time.
20512052 ///
2053+ /// Note: [`reduce()`] can be used to use the first element as the initial
2054+ /// value, if the accumulator type and item type is the same.
2055+ ///
20522056 /// # Note to Implementors
20532057 ///
20542058 /// Several of the other (forward) methods have default implementations in
@@ -2104,6 +2108,8 @@ pub trait Iterator {
21042108 /// // they're the same
21052109 /// assert_eq!(result, result2);
21062110 /// ```
2111+ ///
2112+ /// [`reduce()`]: Iterator::reduce
21072113 #[ doc( alias = "reduce" ) ]
21082114 #[ doc( alias = "inject" ) ]
21092115 #[ inline]
@@ -2120,10 +2126,15 @@ pub trait Iterator {
21202126 accum
21212127 }
21222128
2123- /// The same as [`fold()`], but uses the first element in the
2124- /// iterator as the initial value, folding every subsequent element into it.
2125- /// If the iterator is empty, return [`None`]; otherwise, return the result
2126- /// of the fold.
2129+ /// Reduces the elements to a single one, by repeatedly applying a reducing
2130+ /// operation.
2131+ ///
2132+ /// If the iterator is empty, returns [`None`]; otherwise, returns the
2133+ /// result of the reduction.
2134+ ///
2135+ /// For iterators with at least one element, this is the same as [`fold()`]
2136+ /// with the first element of the iterator as the initial value, folding
2137+ /// every subsequent element into it.
21272138 ///
21282139 /// [`fold()`]: Iterator::fold
21292140 ///
@@ -2132,13 +2143,11 @@ pub trait Iterator {
21322143 /// Find the maximum value:
21332144 ///
21342145 /// ```
2135- /// #![feature(iterator_fold_self)]
2136- ///
21372146 /// fn find_max<I>(iter: I) -> Option<I::Item>
21382147 /// where I: Iterator,
21392148 /// I::Item: Ord,
21402149 /// {
2141- /// iter.fold_first (|a, b| {
2150+ /// iter.reduce (|a, b| {
21422151 /// if a >= b { a } else { b }
21432152 /// })
21442153 /// }
@@ -2149,8 +2158,8 @@ pub trait Iterator {
21492158 /// assert_eq!(find_max(b.iter()), None);
21502159 /// ```
21512160 #[ inline]
2152- #[ unstable ( feature = "iterator_fold_self" , issue = "68125 " ) ]
2153- fn fold_first < F > ( mut self , f : F ) -> Option < Self :: Item >
2161+ #[ stable ( feature = "iterator_fold_self" , since = "1.51.0 " ) ]
2162+ fn reduce < F > ( mut self , f : F ) -> Option < Self :: Item >
21542163 where
21552164 Self : Sized ,
21562165 F : FnMut ( Self :: Item , Self :: Item ) -> Self :: Item ,
@@ -2647,7 +2656,7 @@ pub trait Iterator {
26472656 move |x, y| cmp:: max_by ( x, y, & mut compare)
26482657 }
26492658
2650- self . fold_first ( fold ( compare) )
2659+ self . reduce ( fold ( compare) )
26512660 }
26522661
26532662 /// Returns the element that gives the minimum value from the
@@ -2707,7 +2716,7 @@ pub trait Iterator {
27072716 move |x, y| cmp:: min_by ( x, y, & mut compare)
27082717 }
27092718
2710- self . fold_first ( fold ( compare) )
2719+ self . reduce ( fold ( compare) )
27112720 }
27122721
27132722 /// Reverses an iterator's direction.
0 commit comments