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 https://github.com/rust-lang/rust/issues/81819 and applied it here. r? `@thomcc`
This commit is contained in:
commit
b6d1c15076
@ -2431,22 +2431,13 @@ pub trait Iterator {
|
|||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// Find the maximum value:
|
|
||||||
///
|
|
||||||
/// ```
|
/// ```
|
||||||
/// fn find_max<I>(iter: I) -> Option<I::Item>
|
/// let reduced: i32 = (1..10).reduce(|acc, e| acc + e).unwrap();
|
||||||
/// where I: Iterator,
|
/// assert_eq!(reduced, 45);
|
||||||
/// I::Item: Ord,
|
|
||||||
/// {
|
|
||||||
/// iter.reduce(|accum, item| {
|
|
||||||
/// if accum >= item { accum } else { item }
|
|
||||||
/// })
|
|
||||||
/// }
|
|
||||||
/// let a = [10, 20, 5, -23, 0];
|
|
||||||
/// let b: [u32; 0] = [];
|
|
||||||
///
|
///
|
||||||
/// assert_eq!(find_max(a.iter()), Some(&20));
|
/// // Which is equivalent to doing it with `fold`:
|
||||||
/// assert_eq!(find_max(b.iter()), None);
|
/// let folded: i32 = (1..10).fold(0, |acc, e| acc + e);
|
||||||
|
/// assert_eq!(reduced, folded);
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
#[stable(feature = "iterator_fold_self", since = "1.51.0")]
|
#[stable(feature = "iterator_fold_self", since = "1.51.0")]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user