Closed
Description
Playground: https://play.rust-lang.org/?gist=0bde8d89f0ef683de58561b7cae3a096&version=nightly&backtrace=0
Code:
fn foo(b: &mut u64) {
let x = &mut b;
}
fn main() {
let mut x = 42;
foo(&mut x);
}
Current Error:
error: cannot borrow immutable argument `b` as mutable
--> <anon>:2:18
|
1 | fn foo(b: &mut u64) {
| - use `mut b` here to make mutable
2 | let x = &mut b;
| ^ cannot borrow mutably
error: aborting due to previous error
This error is confusing because:
- It refers to an argument of type &mut T as "immutable".
- It explicitly suggests "adding more muts", when the correct solution is to remove the redundant "&mut".
Some of the immediate reactions in #rust include:
[21:48:05] <Ixrec> the message ought to be more like "cannot mutably borrow self because it's already a mutable reference"
[21:49:29] <misdreavus> "hint: self is already &mut Foo, do you mean to make a &mut &mut Foo?"
[21:52:00] <sebk> "self is already &mut" would have explained it for me
I think misdreavus' suggestion might be the best and/or simplest option.