As an exercise in learning Rust I want to implement a generic reverse function which takes a vector or an array as input and returns a new vector or an array with the input elements in the reverse order. The input vector remains untouched.
In other words a function with the following signature…
fn reverse(input: &[T]) -> &[T];
But it seems this is not possible since you cannot return slices from a function. So the next best thing which I can think of is…
fn reverse(input: Box<[T]>) -> Box<[T]>;
But now this will not work with arrays and even if it could it will lead to an unnecessary heap allocation.
How would you implement such a
Note: Since I have already specified that the input vector/array remains undisturbed we will have to assume T is cloneable (copyable?).