Function rufl::collection::count_by
source · pub fn count_by<C: AsRef<[T]>, T>(
collection: C,
predicate: impl Fn(&T, usize) -> bool
) -> usize
Expand description
Iterates over elements of collection with predicate function, returns the number of all matched elements.
- predicate function signature:
fn(item: &T, index: usize) -> bool
§Arguments
-
collection
- The collection to iterate over. -
predicate
- The check function invoked per iteration.
§Returns
Returns the number of all elements which pass the predicate check.
§Examples
use rufl::collection;
assert_eq!(2, collection::count_by([1, 2, 3, 4, 5], &|n: &i32, _i: usize| *n < 3));
assert_eq!(3, collection::count_by([1, 2, 3, 4, 5].to_vec(), &|n: &i32, _i: usize| *n % 2 != 0));