pub fn min_by<T, F>(collection: &[T], comparison: F) -> Option<T>Expand description
Find the minimum element in a collection based on a custom comparison function.
If the collection is empty, returns None.
§Arguments
collection- A slice of items.comparison- A function that takes two items and returnstrueif the first item is considered less than the second.
§Returns
Option<T>- The minimum item in the collection based on the comparison function, orNoneif the collection is empty.
§Examples
use lowdash::min_by;
let numbers = vec![5, 3, 8, 1, 4];
let min = min_by(&numbers, |a, b| a < b);
assert_eq!(min, Some(1));
let strings = vec!["apple", "banana", "cherry"];
let min = min_by(&strings, |a, b| a.len() < b.len());
assert_eq!(min, Some("apple"));
let empty: Vec<i32> = vec![];
let min = min_by(&empty, |a, b| a < b);
assert_eq!(min, None);