Stabilize slice_sort_by_cached_key by scottmcm · Pull Request #58074 · rust-lang/rust (original) (raw)
I was going to ask on the tracking issue (#34447), but decided to just send this and hope for an FCP here. The method was added last March by #48639.
Signature: https://doc.rust-lang.org/std/primitive.slice.html#method.sort_by_cached_key
impl [T] { pub fn sort_by_cached_key<K, F>(&mut self, f: F) where F: FnMut(&T) -> K, K: Ord; }
That's an identical signature to the existing sort_by_key
, so I think the questions are just naming, implementation, and the usual "do we want this?".
The implementation seems to have proven its use in rustc at least, which many uses: https://github.com/rust-lang/rust/search?l=Rust&q=sort_by_cached_key
(I'm asking because it's exactly what I just needed the other day:
all_positions.sort_by_cached_key(|&n|
data::CITIES.iter()
.map(|x| *metric_closure.get_edge(n, x.pos).unwrap())
.sum::<usize>()
);
since caching that key is a pretty obviously good idea.)
Closes #34447