pub fn first_or<T>(collection: &[T], fallback: T) -> Twhere
T: Clone,Expand description
Returns the first item from the collection. If the collection is empty, returns the provided fallback value.
§Arguments
collection- A slice of items.fallback- The value to return if the collection is empty.
§Returns
T- The first item in the collection. If the collection is empty, returns thefallbackvalue.
§Examples
use lowdash::first_or;
let numbers = vec![1, 2, 3];
let first_num = first_or(&numbers, 10);
assert_eq!(first_num, 1);
let empty: Vec<i32> = vec![];
let first_num = first_or(&empty, 10);
assert_eq!(first_num, 10);use lowdash::first_or;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
];
let first_person = first_or(&people, Person { name: "Default".to_string(), age: 0 });
assert_eq!(first_person, Person { name: "Alice".to_string(), age: 25 });
let empty_people: Vec<Person> = vec![];
let first_person = first_or(&empty_people, Person { name: "Default".to_string(), age: 0 });
assert_eq!(first_person, Person { name: "Default".to_string(), age: 0 });