L

Тут есть какая-то очень простая, но мощная идея, которую я пока не могу внятно объяснить.
Size: a a a
L
R
R
R
L
C
C
fn main() {
let mut v = vec![10, 4, 5, 34, 56, 2100, 6, 3, 4, 100];
selection_sort(&v);
}
fn find_smallest(arr: &Vec<i32>) -> usize {
let mut smallest = arr[0];
let mut smallest_index = 0;
for i in 1..arr.len() {
if arr[i] < smallest {
smallest = arr[i];
smallest_index = i;
}
}
smallest_index as usize
}
fn selection_sort(arr: &mut Vec<i32>) -> Vec<i32> {
let mut new_arr:Vec<i32> = vec![0; 0];
for _i in 0..arr.len() {
let smallest = find_smallest(arr);
new_arr.push(arr.remove(smallest));
}
new_arr
}
C
4 | let v_sorted: Vec<i32> = selection_sort(&v);
| ^^ types differ in mutability
|
= note: expected mutable reference `&mut std::vec::Vec<i32>`
found reference `&std::vec::Vec<i32>`
V
V
V
C
C
R
&v
R
v: Vec<_>
значит &v: &Vec<_>
V
R
ВМ