External Publication
Visit Post

`<[T]>::sort_by_index` and `<[T]>::sort_by_key_and_index`

Rust Internals [Unofficial] April 16, 2026
Source

I imagine that keys.sort_values(&mut values) would be:

keys.manual_sort_unstable(|keys, i, j| {
    mem::swap(&mut keys[i], &mut keys[j]);
    mem::swap(&mut values[i], &mut values[j]);
});

and then because the swap would be part of the std function instead of a custom closure, std can know that the slices won't change during sorting, so bounds checks can be skipped, like so:

keys.manual_sort_unstable(|keys, i, j| unsafe {
    keys.swap_unchecked(i, j);
    values.swap_unchecked(i, j);
});

Discussion in the ATmosphere

Loading comments...