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§
Sourcefn try_all<E, F>(&mut self, predicate: F) -> Result<bool, E>
fn try_all<E, F>(&mut self, predicate: F) -> 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);
Sourcefn try_any<E, F>(&mut self, predicate: F) -> Result<bool, E>
fn try_any<E, F>(&mut self, predicate: F) -> 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);
Sourcefn try_position<E, F>(&mut self, predicate: F) -> Result<Option<usize>, E>
fn try_position<E, F>(&mut self, predicate: F) -> Result<Option<usize>, 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);
Sourcefn try_rposition<E, F>(&mut self, predicate: F) -> Result<Option<usize>, E>where
Self: Sized + ExactSizeIterator + DoubleEndedIterator,
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>,
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);