Function predicates::iter::in_iter

source ·
pub fn in_iter<I, T>(iter: I) -> InPredicate<T>
where T: PartialEq + Debug, I: IntoIterator<Item = T>,
Expand description

Creates a new predicate that will return true when the given variable is contained with the set of items provided.

Note that this implementation places the fewest restrictions on the underlying Item type at the expense of having the least performant implementation (linear search). If the type to be searched is Hash + Eq, it is much more efficient to use HashableInPredicate and in_hash. The implementation-specific predicates will be deprecated when Rust supports trait specialization.

If you need to optimize this

  • Type is Ord, call sort() on this predicate.
  • Type is Hash, replace in_iter with in_hash.


use predicates::prelude::*;

let predicate_fn = predicate::in_iter(vec![1, 3, 5]);
assert_eq!(true, predicate_fn.eval(&1));
assert_eq!(false, predicate_fn.eval(&2));
assert_eq!(true, predicate_fn.eval(&3));

let predicate_fn = predicate::in_iter(vec!["a", "c", "e"]);
assert_eq!(true, predicate_fn.eval("a"));
assert_eq!(false, predicate_fn.eval("b"));
assert_eq!(true, predicate_fn.eval("c"));