Skip to content

Improve diagnostic for missing IndexMut: HashMap #100873

Closed
@estebank

Description

@estebank

The following is invalid

let mut unique_things: HashMap<usize, bool>  = HashMap::new();
unique_things[&5] = false;
error[E0594]: cannot assign to data in an index of `HashMap<usize, bool>`
   --> src/main.rs:147:2
    |
147 |     unique_things[&5] = false;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot assign
    |
    = help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `HashMap<usize, bool>`

Because the entry &5 must exist before it can be assigned to. We should propose the use of the Entry APIs or .insert method.

https://users.rust-lang.org/t/problematic-errormessage-indexmut-is-required-to-modify-indexed-content/80158

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions