[src]

Function std::result::collect

pub fn collect<T, E, Iter: Iterator<Result<T, E>>, V: FromIterator<T>>(iter: Iter) -> Result<V, E>

Takes each element in the Iterator: if it is an Err, no further elements are taken, and the Err is returned. Should no Err occur, a vector containing the values of each Result is returned.

Here is an example which increments every integer in a vector, checking for overflow:

fn inc_conditionally(x: uint) -> Result<uint, &'static str> {
    if x == uint::MAX { return Err("overflow"); }
    else { return Ok(x+1u); }
}
let v = [1u, 2, 3];
let res = collect(v.iter().map(|&x| inc_conditionally(x)));
assert!(res == Ok(~[2u, 3, 4]));