try_all 0.0.2

Extends iterators with `try_all` to convert iterator of results into result of iterator of okays
Documentation
  • Coverage
  • 57.14%
    8 out of 14 items documented4 out of 4 items with examples
  • Size
  • Source code size: 13.29 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.5 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • E-gy/try_all
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • E-gy

crates.io

Rust iterator extensions to operate on Results 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 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_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())
}