@@ -164,12 +164,13 @@ where
164
164
/// element is encountered:
165
165
///
166
166
/// ```
167
+ /// let f = |&x: &i32| if x < 0 { Err("Negative element found") } else { Ok(x) };
167
168
/// 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();
172
170
/// 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"));
173
174
/// ```
174
175
fn sum < I > ( iter : I ) -> Result < T , E >
175
176
where
@@ -187,6 +188,20 @@ where
187
188
/// Takes each element in the [`Iterator`]: if it is an [`Err`], no further
188
189
/// elements are taken, and the [`Err`] is returned. Should no [`Err`]
189
190
/// 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
+ /// ```
190
205
fn product < I > ( iter : I ) -> Result < T , E >
191
206
where
192
207
I : Iterator < Item = Result < U , E > > ,
@@ -213,6 +228,9 @@ where
213
228
/// let words = vec!["have", "a", "great", "day"];
214
229
/// let total: Option<usize> = words.iter().map(|w| w.find('a')).sum();
215
230
/// 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);
216
234
/// ```
217
235
fn sum < I > ( iter : I ) -> Option < T >
218
236
where
@@ -230,6 +248,20 @@ where
230
248
/// Takes each element in the [`Iterator`]: if it is a [`None`], no further
231
249
/// elements are taken, and the [`None`] is returned. Should no [`None`]
232
250
/// 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
+ /// ```
233
265
fn product < I > ( iter : I ) -> Option < T >
234
266
where
235
267
I : Iterator < Item = Option < U > > ,
0 commit comments