first

Function first 

Source
pub fn first<T>(collection: &[T]) -> (T, bool)
where T: Clone + Default,
Expand description

Returns the first item from the collection. If the collection is empty, returns the default value of T and false.

§Arguments

  • collection - A slice of items.

§Returns

  • (T, bool) - A tuple containing the first item and true. If the collection is empty, returns (T::default(), false).

§Examples

use lowdash::first;

let numbers = vec![1, 2, 3];
let (first_num, exists) = first(&numbers);
assert_eq!(first_num, 1);
assert!(exists);

let empty: Vec<i32> = vec![];
let (first_num, exists) = first(&empty);
assert_eq!(first_num, 0); // i32::default() is 0
assert!(!exists);