Trait try_all::TryAll [−][src]
pub trait TryAll { type AllOrFirst; fn try_all(self) -> Self::AllOrFirst; }
Expand description
Trait providing try extensions to Iterators.
Associated Types
type AllOrFirst
[src]
type AllOrFirst
[src]Required methods
fn try_all(self) -> Self::AllOrFirst
[src]
fn try_all(self) -> Self::AllOrFirst
[src]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 collect
ing 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.
Implementors
impl<I> TryAll for I where
I: Iterator,
(I, <I as Iterator>::Item): TryAllHack<Iter = I>,
[src]
impl<I> TryAll for I where
I: Iterator,
(I, <I as Iterator>::Item): TryAllHack<Iter = I>,
[src]