Skip to content

Commit 07af691

Browse files
authored
Rollup merge of rust-lang#106434 - clubby789:document-sum-result, r=the8472
Document `Iterator::sum/product` for Option/Result Closes rust-lang#105266 We already document the similar behavior for `collect()` so I believe it makes sense to add this too. The Option/Result implementations *are* documented on their respective pages and the page for `Sum`, but buried amongst many other trait impls which doesn't make it very discoverable. ````@rustbot```` label +A-docs
2 parents 4f0c7ba + f321144 commit 07af691

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

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

+36-4
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,13 @@ where
164164
/// element is encountered:
165165
///
166166
/// ```
167+
/// let f = |&x: &i32| if x < 0 { Err("Negative element found") } else { Ok(x) };
167168
/// let v = vec![1, 2];
168-
/// let res: Result<i32, &'static str> = v.iter().map(|&x: &i32|
169-
/// if x < 0 { Err("Negative element found") }
170-
/// else { Ok(x) }
171-
/// ).sum();
169+
/// let res: Result<i32, _> = v.iter().map(f).sum();
172170
/// assert_eq!(res, Ok(3));
171+
/// let v = vec![1, -2];
172+
/// let res: Result<i32, _> = v.iter().map(f).sum();
173+
/// assert_eq!(res, Err("Negative element found"));
173174
/// ```
174175
fn sum<I>(iter: I) -> Result<T, E>
175176
where
@@ -187,6 +188,20 @@ where
187188
/// Takes each element in the [`Iterator`]: if it is an [`Err`], no further
188189
/// elements are taken, and the [`Err`] is returned. Should no [`Err`]
189190
/// occur, the product of all elements is returned.
191+
///
192+
/// # Examples
193+
///
194+
/// This multiplies each number in a vector of strings,
195+
/// if a string could not be parsed the operation returns `Err`:
196+
///
197+
/// ```
198+
/// let nums = vec!["5", "10", "1", "2"];
199+
/// let total: Result<usize, _> = nums.iter().map(|w| w.parse::<usize>()).product();
200+
/// assert_eq!(total, Ok(100));
201+
/// let nums = vec!["5", "10", "one", "2"];
202+
/// let total: Result<usize, _> = nums.iter().map(|w| w.parse::<usize>()).product();
203+
/// assert!(total.is_err());
204+
/// ```
190205
fn product<I>(iter: I) -> Result<T, E>
191206
where
192207
I: Iterator<Item = Result<U, E>>,
@@ -213,6 +228,9 @@ where
213228
/// let words = vec!["have", "a", "great", "day"];
214229
/// let total: Option<usize> = words.iter().map(|w| w.find('a')).sum();
215230
/// assert_eq!(total, Some(5));
231+
/// let words = vec!["have", "a", "good", "day"];
232+
/// let total: Option<usize> = words.iter().map(|w| w.find('a')).sum();
233+
/// assert_eq!(total, None);
216234
/// ```
217235
fn sum<I>(iter: I) -> Option<T>
218236
where
@@ -230,6 +248,20 @@ where
230248
/// Takes each element in the [`Iterator`]: if it is a [`None`], no further
231249
/// elements are taken, and the [`None`] is returned. Should no [`None`]
232250
/// occur, the product of all elements is returned.
251+
///
252+
/// # Examples
253+
///
254+
/// This multiplies each number in a vector of strings,
255+
/// if a string could not be parsed the operation returns `None`:
256+
///
257+
/// ```
258+
/// let nums = vec!["5", "10", "1", "2"];
259+
/// let total: Option<usize> = nums.iter().map(|w| w.parse::<usize>().ok()).product();
260+
/// assert_eq!(total, Some(100));
261+
/// let nums = vec!["5", "10", "one", "2"];
262+
/// let total: Option<usize> = nums.iter().map(|w| w.parse::<usize>().ok()).product();
263+
/// assert_eq!(total, None);
264+
/// ```
233265
fn product<I>(iter: I) -> Option<T>
234266
where
235267
I: Iterator<Item = Option<U>>,

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

+6
Original file line numberDiff line numberDiff line change
@@ -3448,6 +3448,9 @@ pub trait Iterator {
34483448
///
34493449
/// An empty iterator returns the zero value of the type.
34503450
///
3451+
/// `sum()` can be used to sum any type implementing [`Sum`][`core::iter::Sum`],
3452+
/// including [`Option`][`Option::sum`] and [`Result`][`Result::sum`].
3453+
///
34513454
/// # Panics
34523455
///
34533456
/// When calling `sum()` and a primitive integer type is being returned, this
@@ -3478,6 +3481,9 @@ pub trait Iterator {
34783481
///
34793482
/// An empty iterator returns the one value of the type.
34803483
///
3484+
/// `product()` can be used to multiply any type implementing [`Product`][`core::iter::Product`],
3485+
/// including [`Option`][`Option::product`] and [`Result`][`Result::product`].
3486+
///
34813487
/// # Panics
34823488
///
34833489
/// When calling `product()` and a primitive integer type is being returned,

0 commit comments

Comments
 (0)