In C, I can mutably iterate over an array in a nested fashion, using indices. In Rust, I can pretty much do the same using indices, but what if I want to use an iterator instead of indices?
The following piece of code for example compiles successfully since both borrows are immutable:
let xs = [0, 1, 2];
for x in &xs {
for y in &xs {
println!("x={} y={}", *x, *y);
}
}
But what if I want to use a mutable iterator?
let mut xs = [0, 1, 2];
for x in &mut xs {
*x += 1;
for y in &mut xs {
*y += 1;
println!("x={} y={}", *x, *y);
}
}
This results in:
error[E0499]: cannot borrow `xs` as mutable more than once at a time
I understand the need to channel write access to data, but I also wonder how an experienced Rust user would achieve that using only iterators -- let's say indices are out of the picture just for educational purposes.