Skip to content

Commit

Permalink
Add improved doc example for Sum<Option<T>>
Browse files Browse the repository at this point in the history
  • Loading branch information
jtdowney committed Mar 21, 2019
1 parent 8c69876 commit 422a4c0
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/libcore/iter/traits/accum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,13 @@ where
///
/// # Examples
///
/// This sums up every integer in a vector, rejecting the sum if a negative
/// element is encountered:
/// This sums up the position of the character 'a' in a vector of strings,
/// if a word did not have the character 'a' the operation returns `None`:
///
/// ```
/// let v = vec![1, 2];
/// let res: Option<i32> = v.iter().map(|&x: &i32|
/// if x < 0 { None }
/// else { Some(x) }
/// ).sum();
/// assert_eq!(res, Some(3));
/// let words = vec!["have", "a", "great", "day"];
/// let total: Option<usize> = words.iter().map(|w| w.find('a')).sum();
/// assert_eq!(total, Some(5));
/// ```
fn sum<I>(iter: I) -> Option<T>
where
Expand Down

0 comments on commit 422a4c0

Please sign in to comment.