diff options
| author | Guillaume Gomez <guillaume.gomez@huawei.com> | 2022-09-28 23:01:10 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume.gomez@huawei.com> | 2022-09-29 00:44:53 +0200 |
| commit | 49b25d341286499dd1e30f5825398a8c9cd3a54a (patch) | |
| tree | d29fc75f03ee9721b9fd4aad48d04eb6d3886223 | |
| parent | a925e203d199daac8589efa2b5e9849474f1e3a1 (diff) | |
| download | rust-49b25d341286499dd1e30f5825398a8c9cd3a54a.tar.gz rust-49b25d341286499dd1e30f5825398a8c9cd3a54a.zip | |
Improve example of Iterator::reduce
| -rw-r--r-- | library/core/src/iter/traits/iterator.rs | 19 |
1 files changed, 5 insertions, 14 deletions
diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index e26920b25cc..f6e6732b0e3 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -2431,22 +2431,13 @@ pub trait Iterator { /// /// # Example /// - /// Find the maximum value: - /// /// ``` - /// fn find_max<I>(iter: I) -> Option<I::Item> - /// where I: Iterator, - /// I::Item: Ord, - /// { - /// iter.reduce(|accum, item| { - /// if accum >= item { accum } else { item } - /// }) - /// } - /// let a = [10, 20, 5, -23, 0]; - /// let b: [u32; 0] = []; + /// let reduced: i32 = (1..10).reduce(|acc, e| acc + e).unwrap(); + /// assert_eq!(reduced, 45); /// - /// assert_eq!(find_max(a.iter()), Some(&20)); - /// assert_eq!(find_max(b.iter()), None); + /// // Which is equivalent to doing it with `fold`: + /// let folded: i32 = (1..10).fold(0, |acc, e| acc + e); + /// assert_eq!(reduced, folded); /// ``` #[inline] #[stable(feature = "iterator_fold_self", since = "1.51.0")] |
