Macro konst::min_by

source ·
macro_rules! min_by {
    ($left:expr, $right:expr, $($comparator:tt)*) => { ... };
}
Expand description

Const equivalent of std::cmp::min_by

Returns the $left argument if both compare equal.

§Example

// passing a pseudo-closure as the comparator
const AAA: u32 = konst::min_by!(3u32, 10, |&l, &r| konst::const_cmp!(l, r / 4));
assert_eq!(AAA, 10);

// Both arguments compare equal, so the first argument (`12`) is returned.
const MIN_OF_EQ: u32 = konst::min_by!(12, 6, |l, r: &u32| konst::const_cmp!(*l % 3, *r % 3));
assert_eq!(MIN_OF_EQ, 12);

const fn cmp_len(l: &str, r: &str) -> std::cmp::Ordering {
    konst::const_cmp!(l.len(), r.len())
}

// passing a function as the comparator
const BBB: &str = konst::min_by!("he", "bar", cmp_len);
assert_eq!(BBB, "he");