I have a large array which I am slicing using split_at()
:
let (left, right) = large_array.split_at(i);
I am doing some operation on left
and then continuing to slice right
in a while loop until right
is empty.
// test vector
let v: Vec<u8> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let (left, right) = v.split_at(1); // left = [1], right = [2,3,4 ... 10]
let j: u8 = left[0];
for i in 1..10 {
println!("{} {}", j, right.len()); // prints "1 9" on all iterations
let (left, right) = right.split_at(1);
let j: u8 = left[0];
println!("{} {}", j, right.len()); // prints "2 8" on all iterations
}
Although the length of right
changes from 9 to 8 during the first iteration, on the second iteration it returns back to being 9. What's causing this?