[−][src]Function out::slice::max_by_cached_key
pub fn max_by_cached_key<T, K: Ord>(
v: &mut [T],
n: usize,
f: impl FnMut(&T) -> K
) -> &mut [T]
Returns the n
largest items with a key extraction function.
The key function is called only once per element, but for simple key functions sort_by_key
is likely to be faster.
This sort is stable, i.e. it preserves the order of equal elements.
Panics
Panics if n > len
.
Examples
let mut v = [-5_i32, 4, 1, -3, 2]; let max = out::slice::max_by_cached_key(&mut v, 3, |a| a.abs()); assert_eq!(max, [-3, 4, -5]);