You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
pubfnfoo(muta:&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 ais 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?
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:which when compiled yields the error message:
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?cc @jonathandturner
The text was updated successfully, but these errors were encountered: