Skip to main content

smallest

Function smallest 

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

Compares and returns the minimum of the provided values.

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

Internally uses Ord::min.

§Examples

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

assert_eq!(cmp::smallest(1), 1);
assert_eq!(cmp::smallest(1, 2), 1);
assert_eq!(cmp::smallest(3, 2, 1), 1);
assert_eq!(cmp::smallest(1, 2, 3, 4), 1);
#![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::smallest(Equal("v1"), Equal("v2")).0, "v1");

§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 min itself variadic instead.