Trait TryContinue

Source
pub trait TryContinue<T, E>: Iterator<Item = Result<T, E>> {
    // Provided method
    fn try_continue<F, R>(self, f: F) -> Result<R, E>
       where Self: Sized,
             F: FnOnce(&mut TryContinueIter<Self, E>) -> R { ... }
}
Expand description

Provides the TryContinue::try_continue method, which allows use of the iterator API after mapping to fallible functions.

Provided Methods§

Source

fn try_continue<F, R>(self, f: F) -> Result<R, E>
where Self: Sized, F: FnOnce(&mut TryContinueIter<Self, E>) -> R,

Allows one to continue processing an iterator of Result<T, _>, as if it were a Result<T>, provided that all of the elements are Ok. The iterator will short-circuit if an Err element is encountered.

This is particularly useful if you need to map to a fallible function, and would like to continue using the iterator API to process the elements, but still know if the mapped function fails.

§Errors

The Result will only return an error if the given function returns one.

§Examples
use try_continue::TryContinue;

let elements = vec!["1", "2", "3", "4"];
let total = elements
    .into_iter()
    .map(str::parse::<u8>)
    .try_continue(|iter| iter.sum());

assert_eq!(10_u8, total.unwrap());
use try_continue::TryContinue;

let elements = vec!["1", "2", "three", "4"];
let total = elements
    .into_iter()
    .map(str::parse::<u8>)
    .try_continue(|iter| iter.sum::<u8>());

assert!(total.is_err());

Implementors§

Source§

impl<T, E, I: Iterator<Item = Result<T, E>>> TryContinue<T, E> for I