Function std::option::collect
pub fn collect<T, Iter: Iterator<Option<T>>, V: FromIterator<T>>(iter: Iter) -> Option<V>
Takes each element in the Iterator
: if it is None
, no further
elements are taken, and the None
is returned. Should no None
occur, a
vector containing the values of each Option
is returned.
Here is an example which increments every integer in a vector, checking for overflow:
fn inc_conditionally(x: uint) -> Option<uint> { if x == uint::MAX { return None; } else { return Some(x+1u); } } let v = [1u, 2, 3]; let res = collect(v.iter().map(|&x| inc_conditionally(x))); assert!(res == Some(~[2u, 3, 4]));