Closed
Description
It is a common mistake, especially for beginners, to attempt to reborrow function arguments when calling other functions.
I tried this code:
struct T;
fn foo(x: &mut T) {}
fn bar(x: &mut T) {
foo(&mut x)
}
I expected to see this happen: Compile error following what the above code does if foo(&x)
is attempted instead.
error[Exxxx]: mismatched types
--> src/lib.rs:6:9
|
6 | foo(&mut x)
| ^^ expected `&mut T`, found `&mut &mut T`
|
= note; you don't need to use `&mut` when you already have a mutable reference
Instead, this happened: Misleading error.
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
--> src/lib.rs:6:9
|
5 | fn bar(x: &mut T) {
| - help: consider changing this to be mutable: `mut x`
6 | foo(&mut x)
| ^^^^^^ cannot borrow as mutable
Meta
1.45.2 srable & 1.47.0 nightly on the playground