fn change(a: &mut i32, b: &mut i32) { let c = *a; *a = *b; *b = c; } fn main() { let mut v = vec![1, 2, 3]; change(&mut v[0], &mut v[1]); } When I compile the code above, it has the error:
error[E0499]: cannot borrow `v` as mutable more than once at a time --> src/main.rs:9:32 | 9 | change(&mut v[0], &mut v[1]); | - ^ - first borrow ends here | | | | | second mutable borrow occurs here | first mutable borrow occurs here Why does the compiler prohibit it? v[0] and v[1] occupy different memory positions, so it's not dangerous to use these together. And what should I do if I come across this problem?
<[T]>::swap().