pub fn filter<'a, T, F>(collection: &'a [T], predicate: F) -> Vec<&'a T>Expand description
Filter items from a collection that satisfy a predicate.
This function iterates over a collection and returns a new vector containing
all items for which the predicate returns true.
§Arguments
collection- A slice of items.predicate- A function that takes an item and its index, returning a boolean.
§Returns
Vec<&T>- A vector of references to items that satisfy the predicate.
§Examples
use lowdash::filter;
let numbers = vec![1, 2, 3, 4, 5];
let result = filter(&numbers, |x, _| *x % 2 == 0);
assert_eq!(result, vec![&2, &4]);use lowdash::filter;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
let result = filter(&people, |p, _| p.age > 30);
assert_eq!(result, vec![&people[2]]);