pub trait TryFilter<O, E>: Sized {
// Required methods
fn try_filter_ok<F>(self, _: F) -> TryFilterOk<Self, F> ⓘ
where F: FnMut(&O) -> Result<bool, E>;
fn try_filter_err<F>(self, _: F) -> TryFilterErr<Self, F> ⓘ
where F: FnMut(&E) -> Result<bool, E>;
}
Expand description
Extension for Iterator<Item = Result<O, E>>
to filter the Ok() and leaving the Err() as
is, but allowing the filter to return a Result<bool, E>
itself
Required Methods§
Sourcefn try_filter_ok<F>(self, _: F) -> TryFilterOk<Self, F> ⓘ
fn try_filter_ok<F>(self, _: F) -> TryFilterOk<Self, F> ⓘ
Filters every Ok
-value with a function that can return an Err.
Useful when the filter condition uses functions that can fail.
use resiter::try_filter::TryFilter;
use std::str::FromStr;
let v = ["1", "2", "4", "a", "5"]
.iter()
.map(Ok)
.try_filter_ok(|e| usize::from_str(e).map(|n| n < 3))
.collect::<Vec<Result<_, _>>>();
assert_eq!(v.len(), 3);
assert_eq!(v.iter().filter(|x| x.is_ok()).count(), 2);
assert_eq!(v.iter().filter(|x| x.is_err()).count(), 1);
Sourcefn try_filter_err<F>(self, _: F) -> TryFilterErr<Self, F> ⓘ
fn try_filter_err<F>(self, _: F) -> TryFilterErr<Self, F> ⓘ
Filters every Err
-value with a function that can return an Err.
Useful when the filter condition uses functions that can fail.
use resiter::try_filter::TryFilter;
use std::num::ParseIntError;
use std::str::FromStr;
let v = ["1", "2", "4", "a", "5"]
.iter()
.map(|txt| usize::from_str(txt))
.try_filter_err(|_:&ParseIntError| Ok(false))
.collect::<Vec<Result<_, _>>>();
assert_eq!(v.iter().filter(|x| x.is_ok()).count(), 4);
assert_eq!(v.iter().filter(|x| x.is_err()).count(), 0);
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.