min_by

Function min_by 

Source
pub fn min_by<T, F>(collection: &[T], comparison: F) -> Option<T>
where T: Clone, F: Fn(&T, &T) -> bool,
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 returns true if the first item is considered less than the second.

§Returns

  • Option<T> - The minimum item in the collection based on the comparison function, or None if 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);