Skip to content

Commit f321144

Browse files
committed
Add example for Option::product and Result::product
1 parent 01d6c04 commit f321144

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-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>>,

0 commit comments

Comments
 (0)