fn selection_sort(array: &mut[i32]) {
let len = array.len();
for i in 0..len {
let min = (i..len).min_by_key(|x| array[*x])
.unwrap();
array.swap(min, i);
}
}
I want to make this code, now this code only works with Vec but I want this to make generic so it works on all datatypes.
I tried fn selection_sort<F: Ord>(arr: &[T])
but it says
error[E0507]: cannot move out of array[_]
, as array
is a captured vari
able in an FnMut
closure
--> selection_sort.rs:35:44
|
31 | fn selection_sort2<T: Ord>(array: &mut[T]) {
| ----- captured outer variable
...
35 | let min = (i..len).min_by_key(|x| array[*x])
| ^^^^^^^^^ move occurs beca
use array[_]
has type T
, which does not implement the Copy
trait