Trait TryIterator

Source
pub trait TryIterator: Iterator {
    // Provided methods
    fn try_all<E, F>(&mut self, predicate: F) -> Result<bool, E>
       where Self: Sized,
             F: FnMut(Self::Item) -> Result<bool, E> { ... }
    fn try_any<E, F>(&mut self, predicate: F) -> Result<bool, E>
       where Self: Sized,
             F: FnMut(Self::Item) -> Result<bool, E> { ... }
    fn try_position<E, F>(&mut self, predicate: F) -> Result<Option<usize>, E>
       where Self: Sized,
             F: FnMut(Self::Item) -> Result<bool, E> { ... }
    fn try_rposition<E, F>(&mut self, predicate: F) -> Result<Option<usize>, E>
       where Self: Sized + ExactSizeIterator + DoubleEndedIterator,
             F: FnMut(Self::Item) -> Result<bool, E> { ... }
}
Expand description

Implements the following fallible iterator methods:

  • try_all;
  • try_any;
  • try_position;
  • try_rposition.

Prefer importing this trait through the crate prelude:

use try_iterator::prelude::*;

Provided Methods§

Source

fn try_all<E, F>(&mut self, predicate: F) -> Result<bool, E>
where Self: Sized, F: FnMut(Self::Item) -> Result<bool, E>,

Tests if every element of the iterator matches a predicate, stopping at the first error and returning that error.

This can also be thought of as the fallible form of all().

§Examples

Ordinary operation:

use try_iterator::prelude::*;

let items: &[Result<&str, u32>] = &[
    Ok("foo"),
    Ok("foo"),
    Ok("foo"),
];

let res = items.iter()
    .try_all(|item| -> Result<_, u32> {
        let equal = (*item)? == "foo";
        Ok(equal)
    });

assert_eq!(res.is_ok(), true);
assert_eq!(res.unwrap(), true);

Fails the whole operation when an Err is present:

use try_iterator::prelude::*;

let items: &[Result<&str, u32>] = &[
    Ok("foo"),
    Err(4444),
    Ok("foo"),
];

let res = items.iter()
    .try_all(|item| -> Result<_, u32> {
        let equal = (*item)? == "foo";
        Ok(equal)
    });

assert_eq!(res.is_err(), true);
Source

fn try_any<E, F>(&mut self, predicate: F) -> Result<bool, E>
where Self: Sized, F: FnMut(Self::Item) -> Result<bool, E>,

Tests if any element of the iterator matches a predicate, stopping at the first error and returning that error.

This can also be thought of as the fallible form of any().

§Examples

Ordinary operation:

use try_iterator::prelude::*;

let items: &[Result<&str, u32>] = &[
    Ok("foo"),
    Ok("ayy"),
    Ok("bar"),
];

let res = items.iter()
    .try_any(|item| -> Result<_, u32> {
        let equal = (*item)? == "bar";
        Ok(equal)
    });

assert_eq!(res.is_ok(), true);
assert_eq!(res.unwrap(), true);

Fails the whole operation when an Err is present:

use try_iterator::prelude::*;

let items: &[Result<&str, u32>] = &[
    Ok("foo"),
    Err(7777),
    Ok("bar"),
];

let res = items.iter()
    .try_any(|item| -> Result<_, u32> {
        let equal = (*item)? == "bar";
        Ok(equal)
    });

assert_eq!(res.is_err(), true);
Source

fn try_position<E, F>(&mut self, predicate: F) -> Result<Option<usize>, E>
where Self: Sized, F: FnMut(Self::Item) -> Result<bool, E>,

Searches for an element in an iterator, returning its index, stopping at the first error and returning that error.

This can also be thought of as the fallible form of position().

§Examples

Ordinary operation:

use try_iterator::prelude::*;

let items: &[Result<&str, u32>] = &[
    Ok("foo"),
    Ok("ayy"),
    Ok("bar"),
];

let pos = items.iter()
    .try_position(|item| -> Result<_, u32> {
        let equal = (*item)? == "bar";
        Ok(equal)
    });

assert_eq!(pos.is_ok(), true);
assert_eq!(pos.unwrap(), Some(2));

Fails the whole operation when an Err is present:

use try_iterator::prelude::*;

let items: &[Result<&str, u32>] = &[
    Ok("foo"),
    Err(8888),
    Ok("bar"),
];

let pos = items.iter()
    .try_any(|item| -> Result<_, u32> {
        let equal = (*item)? == "bar";
        Ok(equal)
    });

assert_eq!(pos.is_err(), true);
Source

fn try_rposition<E, F>(&mut self, predicate: F) -> Result<Option<usize>, E>
where Self: Sized + ExactSizeIterator + DoubleEndedIterator, F: FnMut(Self::Item) -> Result<bool, E>,

Searches for an element in an iterator from the right, returning its index, stopping at the first error and returning that error.

This can also be thought of as the fallible form of rposition().

§Examples

Ordinary operation:

use try_iterator::prelude::*;

let items: &[Result<&str, u32>] = &[
    Ok("foo"),
    Ok("ayy"),
    Ok("bar"),
];

let pos = items.iter()
    .try_rposition(|item| -> Result<_, u32> {
        let equal = (*item)? == "foo";
        Ok(equal)
    });

assert_eq!(pos.is_ok(), true);
assert_eq!(pos.unwrap(), Some(0));

Fails the whole operation when an Err is present:

use try_iterator::prelude::*;

let items: &[Result<&str, u32>] = &[
    Ok("foo"),
    Err(9999),
    Ok("bar"),
];

let pos = items.iter()
    .try_rposition(|item| -> Result<_, u32> {
        let equal = (*item)? == "foo";
        Ok(equal)
    });

assert_eq!(pos.is_err(), true);

Implementations on Foreign Types§

Source§

impl<'a, I, T> TryIterator for Cloned<I>
where T: 'a + Clone, I: Iterator<Item = &'a T>,

Source§

impl<'a, I, T> TryIterator for Copied<I>
where T: 'a + Copy, I: Iterator<Item = &'a T>,

Source§

impl<'a, T> TryIterator for Iter<'a, T>

Source§

impl<A> TryIterator for Repeat<A>
where A: Clone,

Source§

impl<A, B> TryIterator for Chain<A, B>
where A: Iterator, B: Iterator<Item = <A as Iterator>::Item>,

Source§

impl<A, B> TryIterator for Zip<A, B>
where A: Iterator, B: Iterator,

Source§

impl<A, F> TryIterator for OnceWith<F>
where F: FnOnce() -> A,

Source§

impl<A, F> TryIterator for RepeatWith<F>
where F: FnMut() -> A,

Source§

impl<B, I, F> TryIterator for FilterMap<I, F>
where I: Iterator, F: FnMut(<I as Iterator>::Item) -> Option<B>,

Source§

impl<B, I, F> TryIterator for Map<I, F>
where I: Iterator, F: FnMut(<I as Iterator>::Item) -> B,

Source§

impl<B, I, P> TryIterator for MapWhile<I, P>
where I: Iterator, P: FnMut(<I as Iterator>::Item) -> Option<B>,

Source§

impl<B, I, St, F> TryIterator for Scan<I, St, F>
where I: Iterator, F: FnMut(&mut St, <I as Iterator>::Item) -> Option<B>,

Source§

impl<I> TryIterator for Cycle<I>
where I: Clone + Iterator,

Source§

impl<I> TryIterator for Enumerate<I>
where I: Iterator,

Source§

impl<I> TryIterator for Fuse<I>
where I: Iterator,

Source§

impl<I> TryIterator for Peekable<I>
where I: Iterator,

Source§

impl<I> TryIterator for Rev<I>

Source§

impl<I> TryIterator for Skip<I>
where I: Iterator,

Source§

impl<I> TryIterator for StepBy<I>
where I: Iterator,

Source§

impl<I> TryIterator for Take<I>
where I: Iterator,

Source§

impl<I, F> TryIterator for Inspect<I, F>
where I: Iterator, F: FnMut(&<I as Iterator>::Item),

Source§

impl<I, P> TryIterator for Filter<I, P>
where I: Iterator, P: FnMut(&<I as Iterator>::Item) -> bool,

Source§

impl<I, P> TryIterator for SkipWhile<I, P>
where I: Iterator, P: FnMut(&<I as Iterator>::Item) -> bool,

Source§

impl<I, P> TryIterator for TakeWhile<I, P>
where I: Iterator, P: FnMut(&<I as Iterator>::Item) -> bool,

Source§

impl<I, U> TryIterator for Flatten<I>
where I: Iterator, <I as Iterator>::Item: IntoIterator<IntoIter = U, Item = <U as Iterator>::Item>, U: Iterator,

Source§

impl<I, U, F> TryIterator for FlatMap<I, U, F>
where I: Iterator, U: IntoIterator, F: FnMut(<I as Iterator>::Item) -> U,

Source§

impl<T> TryIterator for Empty<T>

Source§

impl<T> TryIterator for Once<T>

Source§

impl<T, F> TryIterator for FromFn<F>
where F: FnMut() -> Option<T>,

Source§

impl<T, F> TryIterator for Successors<T, F>
where F: FnMut(&T) -> Option<T>,

Implementors§