pub fn find_or_else<'a, T, F>(
collection: &'a [T],
fallback: &'a T,
predicate: F,
) -> &'a TExpand description
Find the first item in a collection that satisfies a predicate. If no item satisfies the predicate, return the fallback value.
§Arguments
collection- A collection of items.fallback- Value to return if no item satisfies the predicate.predicate- A function that takes an item from the collection and returns a boolean.
§Returns
&T- Either the first item that satisfies the predicate or the fallback value.
§Examples
use lowdash::find_or_else;
let numbers = vec![1, 2, 3, 4, 5];
let predicate = |x: &i32| *x == 3;
let result = find_or_else(&numbers, &0, predicate);
assert_eq!(result, &3);use lowdash::find_or_else;
let numbers = vec![10, 20, 30, 40];
let result = find_or_else(&numbers, &0, |x| *x > 50);
assert_eq!(result, &0);use lowdash::find_or_else;
#[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 fallback = Person { name: "Unknown".to_string(), age: 0 };
let result = find_or_else(&people, &fallback, |p| p.age > 30);
assert_eq!(result, &Person { name: "Carol".to_string(), age: 35 });