[−][src]Crate resultit
Rust iterators return Option<Item>
, but what happens if Item
is a Result
that could possibly be Err
? This crate supplies iterator adapters to simplify working with so-called "fallible" iterators. The supplied adapters are independent of each other; you can use the whole crate with use resultit::*;
or just the iterator adapter traits you want with (for instance) use resultit::FlattenResults
. You are also free to take individual files (e.g. flatten_results.rs) and use them in your own source tree without depending on this crate.
Example:
// Use the flatten_results() and stop_after_error() iterator adapters. use resultit::FlattenResults; use resultit::StopAfterError; // Use the TryError convenience/shorthand type. use resultit::TryResult; // Nested vector of results with different error types. let v: Vec<Result<Vec<Result<i32, Error2>>, Error1>> = vec![ Ok(vec![Ok(1), Ok(2)]), Ok(vec![Ok(3), Ok(4)]), Ok(vec![Err(Error2{}), Ok(5)]), Ok(vec![Ok(6), Ok(7)]) ]; // Flatten v, stopping after the first error. let v: Vec<TryResult<i32>> = v.into_iter() // Flatten the inner vectors. .flatten_results() // Flatten/erase error types by converting // Result<Result<i32, Error2>, Error1> to Result<i32, E> // where E is a boxed error trait object. .map(|res| -> TryResult<_> { Ok(res??) } ) // Stop iterating after the first error is encountered. .stop_after_error() // Collect into vector v for this example. // Could just as easily have done try_for_each() or any other adapter. .collect(); println!("{:?}", v); // [Ok(1), Ok(2), Ok(3), Ok(4), Err(Error2)]
Re-exports
pub use flatten_results::FlattenResults; |
pub use stop_after_error::StopAfterError; |
Modules
flatten_results | Iterator adapter to flatten an |
stop_after_error | Iterator adapter to stop iteration after the first error is encountered. See documentation for StopAfterError for details. |
Type Definitions
TryResult | Shorthand for a Result with a boxed error trait. Provided for convenience, not a dependency of any submodule. |