Description
When attempting to mutate a field of a borrowed struct (with a shared reference &
) we get the unhelpful E5094 error :
error[E0594]: cannot assign to field
reagent.unreserved_volume
of immutable binding [emphasis mine]
Sample code :
fn main() {
let foo = &Bar{x: 5}; // or any function that returns a shared reference
foo.x -= 12;
}
struct Bar {
x: i32
}
Error :
| foo.x -= 12;
| ^^^^^^^^^^^ cannot mutably borrow field of immutable binding
Since the compiler complains about a “binding” being immutable, my reflex was to add a mut
at foo
's declaration site :
fn main() {
let mut foo = &Bar{x: 5}; // or any function that returns a shared reference
foo.x -= 12;
}
struct Bar {
x: i32
}
Which didn't change anything. (except adding a warning that I didn't notice)
The problem here, is that the error message is complaining about an immutable binding whereas it should complain about an immutable (or shared) reference.
To add insult to injury, the error E0594 doesn't exist in the error index and rustc --explain E0594
returns the laconic
error: no extended information for E0594
Meta
rustc --version --verbose
:
rustc 1.29.0-nightly (e94df4a 2018-07-31)
binary: rustc
commit-hash: e94df4a
commit-date: 2018-07-31
host: x86_64-unknown-linux-gnu
release: 1.29.0-nightly
LLVM version: 7.0
The behavior is the same on Stable 1.27.2 though.