Skip to content

Commit b6d1c15

Browse files
authored
Rollup merge of #102435 - GuillaumeGomez:improve-iterator-reduce-example, r=thomcc,vacuus
Improve example of Iterator::reduce Fixes #81819. I took your example `@bstrie` from #81819 and applied it here. r? `@thomcc`
2 parents 33553e1 + 49b25d3 commit b6d1c15

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

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

+5-14
Original file line numberDiff line numberDiff line change
@@ -2431,22 +2431,13 @@ pub trait Iterator {
24312431
///
24322432
/// # Example
24332433
///
2434-
/// Find the maximum value:
2435-
///
24362434
/// ```
2437-
/// fn find_max<I>(iter: I) -> Option<I::Item>
2438-
/// where I: Iterator,
2439-
/// I::Item: Ord,
2440-
/// {
2441-
/// iter.reduce(|accum, item| {
2442-
/// if accum >= item { accum } else { item }
2443-
/// })
2444-
/// }
2445-
/// let a = [10, 20, 5, -23, 0];
2446-
/// let b: [u32; 0] = [];
2435+
/// let reduced: i32 = (1..10).reduce(|acc, e| acc + e).unwrap();
2436+
/// assert_eq!(reduced, 45);
24472437
///
2448-
/// assert_eq!(find_max(a.iter()), Some(&20));
2449-
/// assert_eq!(find_max(b.iter()), None);
2438+
/// // Which is equivalent to doing it with `fold`:
2439+
/// let folded: i32 = (1..10).fold(0, |acc, e| acc + e);
2440+
/// assert_eq!(reduced, folded);
24502441
/// ```
24512442
#[inline]
24522443
#[stable(feature = "iterator_fold_self", since = "1.51.0")]

0 commit comments

Comments
 (0)