Skip to main content

CollectErrors

Trait CollectErrors 

pub trait CollectErrors<T, E>: Iterator<Item = Result<T, E>> + Sealed {
    // Required method
    fn collect_errors<C>(self) -> Result<C, ErrorList>
       where C: FromIterator<T>;
}
Expand description

Collect every failure of an iterator of results, instead of stopping at the first one as Iterator::collect does.

use rama_error::CollectErrors;

let parsed: Result<Vec<u8>, _> = ["1", "x", "3", "y"]
    .into_iter()
    .map(str::parse::<u8>)
    .collect_errors();
assert_eq!(parsed.unwrap_err().len(), 2);

let parsed: Vec<u8> = ["1", "2"].into_iter().map(str::parse::<u8>).collect_errors().unwrap();
assert_eq!(parsed, [1, 2]);

Required Methods§

fn collect_errors<C>(self) -> Result<C, ErrorList>
where C: FromIterator<T>,

Collect all Ok values, or all failures when there is at least one.

The whole iterator is consumed either way.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

§

impl<I, T, E> CollectErrors<T, E> for I
where I: Iterator<Item = Result<T, E>>, E: Into<Box<dyn Error + Sync + Send>>,