Skip to content

Possible improvement in mutability error message #36412

Closed
@alexcrichton

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?

cc @jonathandturner

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions