Skip to content

Commit 422a4c0

Browse files
committed
Add improved doc example for Sum<Option<T>>
1 parent 8c69876 commit 422a4c0

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/libcore/iter/traits/accum.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,13 @@ where
302302
///
303303
/// # Examples
304304
///
305-
/// This sums up every integer in a vector, rejecting the sum if a negative
306-
/// element is encountered:
305+
/// This sums up the position of the character 'a' in a vector of strings,
306+
/// if a word did not have the character 'a' the operation returns `None`:
307307
///
308308
/// ```
309-
/// let v = vec![1, 2];
310-
/// let res: Option<i32> = v.iter().map(|&x: &i32|
311-
/// if x < 0 { None }
312-
/// else { Some(x) }
313-
/// ).sum();
314-
/// assert_eq!(res, Some(3));
309+
/// let words = vec!["have", "a", "great", "day"];
310+
/// let total: Option<usize> = words.iter().map(|w| w.find('a')).sum();
311+
/// assert_eq!(total, Some(5));
315312
/// ```
316313
fn sum<I>(iter: I) -> Option<T>
317314
where

0 commit comments

Comments
 (0)