Expand description
Trait to represent types that can be created by summing up an iterator.
This trait is used to implement Iterator::sum()
. Types which implement
this trait can be generated by using the sum()
method on an iterator.
Like FromIterator
, this trait should rarely be called directly.
Required methods
Implementations on Foreign Types
Takes each element in the Iterator
: if it is a None
, no further
elements are taken, and the None
is returned. Should no None
occur, the sum of all elements is returned.
Examples
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 words = vec!["have", "a", "great", "day"];
let total: Option<usize> = words.iter().map(|w| w.find('a')).sum();
assert_eq!(total, Some(5));