Skip to content

Unhelpful error message when trying to add elements to hash map #45491

Closed
@est31

Description

@est31

The following code works fine in cpp:

    std::map<int, int> m;
    m[0] = 0;

In Rust however, you are getting an error:

    let mut f :HashMap<u8, u8> = HashMap::new();
    f[&0] = 0;
error[E0594]: cannot assign to immutable indexed content
 --> src/main.rs:4:2
  |
4 | 	f[&0] = 0;
  | 	^^^^^^^^^ cannot borrow as mutable

The error doesn't really tell you what is indexed immutably. IMO it should explain that hashmaps have the insert function instead and maybe even should suggest it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions