Function std::cmp::min_by

1.53.0 · source ·
pub fn min_by<T, F>(v1: T, v2: T, compare: F) -> Twhere
    F: FnOnce(&T, &T) -> Ordering,
Expand description

返回相对于指定比较函数的两个值中的最小值。

如果比较确定它们相等,则返回第一个参数。

Examples

use std::cmp;

let result = cmp::min_by(-2, 1, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
assert_eq!(result, 1);

let result = cmp::min_by(-2, 3, |x: &i32, y: &i32| x.abs().cmp(&y.abs()));
assert_eq!(result, -2);
Run