2 releases
0.0.2 | May 22, 2021 |
---|---|
0.0.1 | May 22, 2021 |
#2039 in Rust patterns
12KB
97 lines
Rust iterator extensions to operate on Result
s effectively.
try_map_all
and, for now, its friend try_map_all_opt
Applies a closure on all items of the iterator until one fails (or all succeed).
Arguments:
f
: fallible mapping function
Returns: The iterator of all successes, or the first failure.
Examples
Useful for propagating failures from within closures with ?
operator:
fn parse_all_numbers(strs: &Vec<&str>) -> Result<Vec<u64>, std::num::ParseIntError> {
Ok(strs.iter().try_map_all(|s| s.parse())?.collect())
}
or for Option
s:
fn not_zero(is: Vec<u64>) -> Option<Vec<u64>> {
Some(is.iter().try_map_all_opt(|i| if i > 0 { Some(i) } else { None })?.collect())
}
try_all
Ensures that all items of the iterator are Ok, otherwise returns the first failure.
Returns: The iterator of all successes, or the first failure.
Examples:
Useful for propagating failures from within closures with ?
operator
fn parse_all_numbers(strs: &Vec<&str>) -> Result<Vec<u64>, std::num::ParseIntError> {
Ok(strs.iter().map(|s| s.parse()).try_all()?.collect())
}