Trait sp_std::iter::Sum1.12.0[][src]

pub trait Sum<A = Self> {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = A>
; }
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

Method which takes an iterator and generates Self from the elements by “summing up” the items.

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));

Implementors