Expand description
Rust iterator extensions to operate on Result
s effectively.
§try_map_all
and try_map_all_opt
Applies a closure on all items of the iterator until one fails (or all succeed).
fn all_numbers_x2(strs: &Vec<&str>) -> Result<Vec<u64>, std::num::ParseIntError> {
Ok(strs.iter().try_map_all(|s| Ok(s.parse::<u64>()?*2))?.collect())
}
Respectively, for Option
s:
fn not_zero(is: Vec<u64>) -> Option<Vec<u64>> {
Some(is.into_iter().try_map_all_opt(|i| if i > 0 { Some(i) } else { None })?.collect())
}
Note: once #42327 is merged, try_map_all
will be implemented* under one name for all try types.
§try_all
Tries all items of the iterator until one fails (or all succeed).
fn parse_all_numbers(strs: &Vec<&str>) -> Result<Vec<u64>, std::num::ParseIntError> {
Ok(strs.iter().map(|s| s.parse()).try_all()?.collect())
}
Traits§
- TryAll
- Trait providing try extensions to Iterators.
- TryAll
Hack - A hack trait to bypass limitations of type checker.
- TryMap
All - Trait providing try map (result) extensions to Iterators.
- TryMap
AllOption - Trait providing try map (option) extensions to Iterators.