`<[T]>::sort_by_index` and `<[T]>::sort_by_key_and_index`
Rust Internals [Unofficial]
April 17, 2026
I did think of that, but I suspect the T-libs folks won't want the stdlib doing that.
But: this is an expensive check no matter what -- intrinsically has to scan the whole array -- and you might want to use the same permutation multiple times, so maybe a wrapper class something like
struct Permutation<N: usize>([usize; N]);
impl Permutation {
/// Checks that `indices` define a permutation
fn from_indices(indices: [usize; N])
-> Result<Self, NotAPermutationError>;
/// Uses self to permute the first N elements of `s`
/// it is an error if s.len() < N
fn permute<T: Sized>(&self, s: &mut [T]) -> Result<(), TooSmallError>;
}
(API sketch only, type annotations for illustration only and may be bogus)
Discussion in the ATmosphere