pub trait TryAll {
type AllOrFirst;
// Required method
fn try_all(self) -> Self::AllOrFirst;
}Expand description
Trait providing try extensions to Iterators.
Required Associated Types§
type AllOrFirst
Required Methods§
Sourcefn try_all(self) -> Self::AllOrFirst
fn try_all(self) -> Self::AllOrFirst
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 to ensure that all items are valid.
fn parse_all_numbers(strs: &Vec<&str>) -> Result<Vec<u64>, std::num::ParseIntError> {
Ok(strs.iter().map(|s| s.parse()).try_all()?.collect())
}§Equivalence
iter.try_all is equivalent to iter.try_map_all(|t| t), except all try-iable variants are implemented under a single name.
Additionally, it is equivalent to collecting the iterator into Vec, checking that all() are okay, and turning Vec back Vec::into_iter() (plus additional logic if the offending error needs to be retrieved).
Due to the nature of operation, the function has to collect intermediate results. In other words, if production has side effects, expect them for all items until [including] first failure. Additionally, this won’t work on infinite sequences :o.