I have a slice of fixed size arrays (&[[T; N]]
), and I'd like to be able to view this as an array of slices ([&[T]; N]
). The i
-th element of the output array would be a slice of all of the i
-th items from each array in the original slice:
fn magic_operation<T, const N: usize>(input: &[[T; N]]) -> [&[T]; N] {
todo!()
}
let original = &[
[10, 11, 12],
[20, 21, 22],
[30, 31, 32],
[40, 41, 42],
[50, 51, 52],
];
let new_view = magic_operation(original);
assert_eq!(new_view, [
&[10, 20, 30, 40, 50],
&[11, 21, 31, 41, 51],
&[12, 22, 32, 42, 52],
]);
I don't know if this is exposed in any of the stdlib methods, and I don't know enough about unsafe Rust to try and approach this myself. Is this possible to achieve, perhaps with something like using slices with a different memory stride?