Improve example of Iterator::reduce

This commit is contained in:
Guillaume Gomez 2022-09-28 23:01:10 +02:00
parent a925e203d1
commit 49b25d3412

View File

@ -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")]