I have tried to execute the following code, but it doesn't compile because "a vector cannot be borrowed as mutable because it is also borrowed as immutable".
The problem is that I don't see how error message is relevant. I am borrowing immutably array[1].1 and mutably array[1].0 which don't overlap.
Isn't borrow checker smart enough to see that there is no conflict and non-overlapped parts of tuples are accessed?
fn main() {
//everything is ok when tuple is alone
let mut pair : (usize, Vec<usize>) = (0, Vec::new());
println!("{} {:?}", &pair.0, &mut pair.1);
let mut array : Vec<(usize, Vec<usize>)>
= vec![(1, Vec::new()), (2, Vec::new())];
//everything works till here
// but the rest doesn't...
println!("{} {:?}", &array[1].0, &mut array[1].1);
}
I am confused : why can I borrow parts of a single tuple mutably and immutably, but cannot do it when tuple is inside a vector. Is it related to splitting borrows described in Rustonomicon?
And finally, the most important : is there a way to fix this code without separating a vector of tuples into 2 vectors?
While a quick answer to the last question is enough for me, I would appreciate a bit more explanation and also answers to my other questions. And if you have a more idiomatic Rust solution for such problem, that would be great too :)
Thanks in advance for taking your time to help me.