Skip to content

Commit a5c1085

Browse files
authored
Rollup merge of rust-lang#79805 - m-ou-se:iterator-reduce, r=KodrAus
Rename Iterator::fold_first to reduce and stabilize it This stabilizes `#![feature(iterator_fold_self)]`. The name for this function (originally `fold_first`) was still an open question, but the discussion on [the tracking issue](rust-lang#68125) seems to have converged to `reduce`.
2 parents 2153f5d + 24e0940 commit a5c1085

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

compiler/rustc_ast/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#![feature(const_fn_transmute)]
1515
#![feature(const_panic)]
1616
#![feature(crate_visibility_modifier)]
17-
#![feature(iterator_fold_self)]
1817
#![feature(label_break_value)]
1918
#![feature(nll)]
2019
#![feature(or_patterns)]

compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl GenericArgs<'_> {
358358
.iter()
359359
.filter(|arg| !arg.is_synthetic())
360360
.map(|arg| arg.span())
361-
.fold_first(|span1, span2| span1.to(span2))
361+
.reduce(|span1, span2| span1.to(span2))
362362
}
363363

364364
/// Returns span encompassing arguments and their surrounding `<>` or `()`

compiler/rustc_hir/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#![feature(const_fn)] // For the unsizing cast on `&[]`
77
#![feature(const_panic)]
88
#![feature(in_band_lifetimes)]
9-
#![feature(iterator_fold_self)]
109
#![feature(once_cell)]
1110
#![feature(or_patterns)]
1211
#![recursion_limit = "256"]

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

+21-12
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)