I am trying to modify a mutable sums: Vec<i64>
while iterating over it. The loop code is as follows:
for (j, &mut sum) in sums.iter_mut().enumerate() {
if !(j == i) {
sum += n;
}
}
And here is the error I get:
error[E0384]: re-assignment of immutable variable `sum`
--> mini_max_sum.rs:27:17
|
25 | for (j, &mut sum) in sums.iter_mut().enumerate() {
| --- first assignment to `sum`
26 | if !(j == i) {
27 | sum += n;
| ^^^^^^^^ re-assignment of immutable variable
This seems totally arcane to me. Rust lets me mutably borrow sum
from sums
, but the compiler prevents me from actually modifying it. Omitting .enumerate()
does not even alter the resulting error code.
I would like to know how to fix the loop.