find

Function find 

Source
pub fn find<T, F>(collection: &[T], predicate: F) -> Option<&T>
where F: Fn(&T) -> bool,
Expand description

Find the first item in a collection that satisfies a predicate. If no item satisfies the predicate, return None.

§Arguments

  • collection - A collection of items.
  • predicate - A function that takes an item from the collection and returns a boolean.

§Returns

  • Some(&T) - The first item in the collection that satisfies the predicate.
  • None - If no item satisfies the predicate.

§Examples

use lowdash::find;
let numbers = vec![1, 2, 3, 4, 5];
let predicate = |x: &i32| *x == 3;
let result = find(&numbers, predicate);
assert_eq!(result, Some(&3));
use lowdash::find;
let numbers = vec![10, 20, 30, 40];
let result = find(&numbers, |x| *x > 25);
assert_eq!(result, Some(&30));
use lowdash::find;

#[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 = find(&people, |p| p.age > 30);
assert_eq!(result, Some(&Person { name: "Carol".to_string(), age: 35 }));