Let's try to compile this code:
trait Bar {
fn bar(&mut self);
}
fn foo(a1: &mut Bar, j: usize) {
let a = [a1];
a[0].bar(); //compilation ok
a[j % 2].bar();
}
fn main() {}
Compilation error:
error[E0596]: cannot borrow immutable local variable `a` as mutable
--> src/main.rs:8:5
|
6 | let a = [a1];
| - consider changing this to `mut a`
7 | a[0].bar(); //compilation ok
8 | a[j % 2].bar();
| ^ cannot borrow mutably
Why is a[0].bar() OK, but a[j % 2].bar() fails? Is it a compiler bug?