I am trying to update elements of a vector knowing where each other is, i.e. test all pairs of elements (unordered), while changing them. So I started naively writing this:
for x in &mut v {
for y in &mut v {
// ...
}
}
But I cannot mutably borrow the vector twice, plus, I could simply avoid a lot of iterations, writing this:
for x in 0..v.len() - 1 {
for y in x..v.len() - 1 {
let mut xc = &mut v[x];
let yc = &v[y];
// ...
}
}
This doesn't work, because I borrow an immutable and a mutable reference! How can I write this basic kind of loop? (I do need the mutability for at least one of the elements.)