Function rust_examples::collect::collect_initials[][src]

pub fn collect_initials(names: Vec<&str>) -> Option<Vec<char>>
Expand description

Traverse a collection of input values and apply an effect to item that may fail (take the first character from each string slice, resulting in an Option<char>).

Here the magic is the sequencing of these effects via collect. This is possible because std::iter::FromIterator is implemented for the Option type, allowing for the following transformations:

  1. Applying an effect on each value of Vec<&str>, turning it into an Iterator<Item = Option<char>>
  2. Turning these effects “inside out” and collecting the items into Option<Vec<char>>

This efectively realizes Traverse, in this case for a Vec which is a ‘Functor’ (has a map) and an Option which is ‘Applicative Monad’ (by the realization in Rust std lib).