pub fn first_or_empty<T>(collection: &[T]) -> TExpand description
Returns the first item from the collection.
If the collection is empty, returns the default value of T.
§Arguments
collection- A slice of items.
§Returns
T- The first item in the collection. If the collection is empty, returnsT::default().
§Examples
use lowdash::first_or_empty;
let numbers = vec![1, 2, 3];
let first_num = first_or_empty(&numbers);
assert_eq!(first_num, 1);
let empty: Vec<i32> = vec![];
let first_num = first_or_empty(&empty);
assert_eq!(first_num, 0); // i32::default() is 0use lowdash::first_or_empty;
#[derive(Debug, PartialEq, Clone, Default)]
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_empty(&people);
assert_eq!(first_person, Person { name: "Alice".to_string(), age: 25 });
let empty_people: Vec<Person> = vec![];
let first_person = first_or_empty(&empty_people);
assert_eq!(first_person, Person::default());