#iterator #filter #optional

iter-opt-filter

Adds an optional filter to iterators

1 unstable release

0.1.0 May 24, 2021

#1948 in Rust patterns

MIT license

19KB
407 lines

iter-opt-filter   Latest Version Docs

This crate adds an optional filter to iterators. The problem this attempts to solve is the combination of multiple filters that can be enabled/disabled at runtime.

Example

use iter_opt_filter::IteratorOptionalFilterExt;

let mut iter = (0..3)
    .optional_filter(Some(|&item: &usize| item % 2 == 0))
    .optional_filter(None::<fn(&usize) -> bool>)
    .optional_filter(Some(|&item: &usize| item > 1));

assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), None);

lib.rs:

This library adds the optional_filter which is provided for all [Iterators][Iterator] by the [IteratorOptionalFilterExt] extension trait.

The optional_filter takes an Option<fn(&item) -> bool>, which allows for easy conditional filtering. This however comes at a performance cost compared to a normal filter or the inner [Iterator] by itself. But it is generally faster than a Box<dyn Iterator>.

See examples on the optional_filter method.

No runtime deps