@@ -2028,7 +2028,8 @@ pub trait Iterator {
2028
2028
self . try_fold ( ( ) , call ( f) )
2029
2029
}
2030
2030
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.
2032
2033
///
2033
2034
/// `fold()` takes two arguments: an initial value, and a closure with two
2034
2035
/// arguments: an 'accumulator', and an element. The closure returns the value that
@@ -2049,6 +2050,9 @@ pub trait Iterator {
2049
2050
/// may not terminate for infinite iterators, even on traits for which a
2050
2051
/// result is determinable in finite time.
2051
2052
///
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
+ ///
2052
2056
/// # Note to Implementors
2053
2057
///
2054
2058
/// Several of the other (forward) methods have default implementations in
@@ -2104,6 +2108,8 @@ pub trait Iterator {
2104
2108
/// // they're the same
2105
2109
/// assert_eq!(result, result2);
2106
2110
/// ```
2111
+ ///
2112
+ /// [`reduce()`]: Iterator::reduce
2107
2113
#[ doc( alias = "reduce" ) ]
2108
2114
#[ doc( alias = "inject" ) ]
2109
2115
#[ inline]
@@ -2120,10 +2126,15 @@ pub trait Iterator {
2120
2126
accum
2121
2127
}
2122
2128
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.
2127
2138
///
2128
2139
/// [`fold()`]: Iterator::fold
2129
2140
///
@@ -2132,13 +2143,11 @@ pub trait Iterator {
2132
2143
/// Find the maximum value:
2133
2144
///
2134
2145
/// ```
2135
- /// #![feature(iterator_fold_self)]
2136
- ///
2137
2146
/// fn find_max<I>(iter: I) -> Option<I::Item>
2138
2147
/// where I: Iterator,
2139
2148
/// I::Item: Ord,
2140
2149
/// {
2141
- /// iter.fold_first (|a, b| {
2150
+ /// iter.reduce (|a, b| {
2142
2151
/// if a >= b { a } else { b }
2143
2152
/// })
2144
2153
/// }
@@ -2149,8 +2158,8 @@ pub trait Iterator {
2149
2158
/// assert_eq!(find_max(b.iter()), None);
2150
2159
/// ```
2151
2160
#[ 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 >
2154
2163
where
2155
2164
Self : Sized ,
2156
2165
F : FnMut ( Self :: Item , Self :: Item ) -> Self :: Item ,
@@ -2647,7 +2656,7 @@ pub trait Iterator {
2647
2656
move |x, y| cmp:: max_by ( x, y, & mut compare)
2648
2657
}
2649
2658
2650
- self . fold_first ( fold ( compare) )
2659
+ self . reduce ( fold ( compare) )
2651
2660
}
2652
2661
2653
2662
/// Returns the element that gives the minimum value from the
@@ -2707,7 +2716,7 @@ pub trait Iterator {
2707
2716
move |x, y| cmp:: min_by ( x, y, & mut compare)
2708
2717
}
2709
2718
2710
- self . fold_first ( fold ( compare) )
2719
+ self . reduce ( fold ( compare) )
2711
2720
}
2712
2721
2713
2722
/// Reverses an iterator's direction.
0 commit comments