TryMapAllOption

Trait TryMapAllOption 

Source
pub trait TryMapAllOption {
    type Item;

    // Required method
    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.

Required Associated Types§

Required Methods§

Source

fn try_map_all_opt<T>( self, f: impl Fn(Self::Item) -> Option<T>, ) -> Option<IntoIter<T>>

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§