max

Function max 

Source
pub fn max<T>(collection: &[T]) -> Option<T>
where T: PartialOrd + Clone + 'static,
Expand description

Find the maximum element in a collection. If the collection is empty, returns None.

§Arguments

  • collection - A slice of items.

§Returns

  • Option<T> - The maximum item in the collection, or None if the collection is empty.

§Examples

use lowdash::max;
let numbers = vec![5, 3, 8, 1, 4];
let result = max(&numbers);
assert_eq!(result, Some(8));
use lowdash::max;
let strings = vec!["apple", "banana", "cherry"];
let result = max(&strings);
assert_eq!(result, Some("cherry"));
use lowdash::max;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
struct Person {
    name: String,
    age: u32,
}

let people = vec![
    Person { name: "Alice".to_string(), age: 25 },
    Person { name: "Bob".to_string(), age: 30 },
    Person { name: "Cia".to_string(), age: 20 },
];

let result = max(&people);
assert_eq!(
    result,
    Some(Person { name: "Cia".to_string(), age: 20 })
);
use lowdash::max;
let collection = vec![3.14, 2.71, -1.0, 0.0];
let result = max(&collection);
assert_eq!(result, Some(3.14));