Closed
Description
Talking to some folks at RustConf one particularly confusing case was "where do I put mut
?" to get mutability. We've got a few locations, to place it (e.g. on the binding or in the type of a reference), and this can be confusing when you're just starting out. As a specific case, consider:
pub fn foo(mut a: &String) {
a.push_str("foo");
}
which when compiled yields the error message:
error: cannot borrow immutable borrowed content `*a` as mutable
--> foo.rs:2:5
|
2 | a.push_str("foo");
| ^
error: aborting due to previous error
To a beginner this can be quite confusing because a
is mutable, but the fix here is to take &mut String
, not &String
. Perhaps we can detect this and provide a more tailored error message for cases like this?
Activity