Trait try_all::TryMapAllOption [−][src]
pub trait TryMapAllOption { type Item; fn try_map_all_opt<T>(
self,
f: impl Fn(Self::Item) -> Option<T>
) -> Option<IntoIter<T>>; }
Expand description
Trait providing try map (option) extensions to Iterators.
See TryMapAllOption::try_map_all_opt
.
Once Try
(#42327) is stabilized, hopefully the Result
and Option
variants can be merged and generalized.
Associated Types
Required methods
fn try_map_all_opt<T>(
self,
f: impl Fn(Self::Item) -> Option<T>
) -> Option<IntoIter<T>>
[src]
fn try_map_all_opt<T>(
self,
f: impl Fn(Self::Item) -> Option<T>
) -> Option<IntoIter<T>>
[src]Applies a closure on all items of the iterator until one fails (or all succeed).
This is TryMapAll::try_map_all
- Option
edition.
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 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()) }
Due to the nature of operation, the function has to collect intermediate results.
In other words, if f
has side effects, expect them for all applications until [including] first failure.
Additionally, this won’t work on infinite sequences :o.