konst

Macro max_by_key

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

Const equivalent of std::cmp::max_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 $right argument if both compare equal.

ยงExample

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

// Both arguments compare equal, so the second argument (`6`) is returned.
const MAX_OF_EQ: u32 = konst::max_by_key!(12, 6, |x: &u32| *x % 4);
assert_eq!(MAX_OF_EQ, 6);

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