Skip to main content

largest

Function largest 

Source
pub const fn largest<T: Ord>(…: impl LargestArgs<Item = T>) -> T
🔬This is a nightly-only experimental API. (cmp_splat #160728)
Expand description

Compares and returns the maximum of the provided values.

Returns the last argument if the comparison determines them to be equal.

Internally uses Ord::max.

§Examples

#![feature(cmp_splat)]
use std::cmp;

assert_eq!(cmp::largest(1), 1);
assert_eq!(cmp::largest(1, 2), 2);
assert_eq!(cmp::largest(3, 2, 1), 3);
assert_eq!(cmp::largest(1, 2, 3, 4), 4);
#![feature(cmp_splat)]
use std::cmp::{self, Ordering};

#[derive(Eq)]
struct Equal(&'static str);

impl PartialEq for Equal {
    fn eq(&self, other: &Self) -> bool { true }
}
impl PartialOrd for Equal {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(Ordering::Equal) }
}
impl Ord for Equal {
    fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal }
}

assert_eq!(cmp::largest(Equal("v1"), Equal("v2")).0, "v2");

§Stability

This function is added in its current form as an experiment in variadic functions. In a future iteration of the feature, this function may be removed in favour of making max itself variadic instead.