konst

Macro min_by_key

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

Const equivalent of std::cmp::min_by_key

The type returned by the comparator must implement the ConstCmp trait. Non-standard library types must define a const_eq method taking a reference.

Returns the $left argument if both compare equal.

ยงExample

// passing a pseudo-closure as the comparator
const AAA: u32 = konst::min_by_key!(3u32, 10, |x| *x % 4);
assert_eq!(AAA, 10);

// Both arguments compare equal, so the first argument (`16`) is returned.
const MIN_OF_EQ: u32 = konst::min_by_key!(16u32, 8, |x| *x % 4);
assert_eq!(MIN_OF_EQ, 16);

// passing a function as the comparator
const BBB: &str = konst::min_by_key!("foo", "he", str::len);
assert_eq!(BBB, "he");