| Crates.io | try_all |
| lib.rs | try_all |
| version | 0.0.2 |
| created_at | 2021-05-22 13:26:23.101941+00 |
| updated_at | 2021-05-22 14:46:02.374432+00 |
| description | Extends iterators with `try_all` to convert iterator of results into result of iterator of okays |
| homepage | |
| repository | https://github.com/E-gy/try_all |
| max_upload_size | |
| id | 400819 |
| size | 13,667 |
Rust iterator extensions to operate on Results effectively.
try_map_alland, 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 functionReturns: The iterator of all successes, or the first failure.
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 Options:
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_allEnsures that all items of the iterator are Ok, otherwise returns the first failure.
Returns: The iterator of all successes, or the first failure.
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())
}