pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2]
🔬This is a nightly-only experimental API. (
cmp_minmax
)Available on crate feature
mtls
only.Expand description
Returns minimum and maximum values with respect to the specified comparison function.
Returns [v1, v2]
if the comparison determines them to be equal.
The parameter order is preserved when calling the compare
function, i.e. v1
is
always passed as the first argument and v2
as the second.
§Examples
#![feature(cmp_minmax)]
use std::cmp;
let abs_cmp = |x: &i32, y: &i32| x.abs().cmp(&y.abs());
assert_eq!(cmp::minmax_by(-2, 1, abs_cmp), [1, -2]);
assert_eq!(cmp::minmax_by(-1, 2, abs_cmp), [-1, 2]);
assert_eq!(cmp::minmax_by(-2, 2, abs_cmp), [-2, 2]);
// You can destructure the result using array patterns
let [min, max] = cmp::minmax_by(-42, 17, abs_cmp);
assert_eq!(min, 17);
assert_eq!(max, -42);