I'm going to switch elements in a Vec, but my solution has ownership problem, Is my code totally wrong?
Given [1, 2, 3, 4, 5, 6]
, the expected output is [4, 5, 6, 1, 2, 3]
.
fn switch(nums: &mut Vec<i32>, k: i32) {
let t = k as usize;
let v1 = &nums[..t];
nums.drain(t..);
nums.extend_from_slice(v1);
}
error[E0502]: cannot borrow `*nums` as mutable because it is also borrowed as immutable
--> src/main.rs:7:5
|
6 | let v1 = &nums[..t];
| ---- immutable borrow occurs here
7 | nums.extend_from_slice(v1);
| ^^^^^-----------------^^^^
| | |
| | immutable borrow later used by call
| mutable borrow occurs here