Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
est31 opened this issue Oct 24, 2017 · 3 comments
Closed

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

est31 opened this issue Oct 24, 2017 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@est31
Copy link
Member

est31 commented Oct 24, 2017

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.

@leonardo-m
Copy link

That functionality was removed:
#23559

This commit removes the IndexMut impls on HashMap and BTreeMap, in order to future-proof the API against the eventual inclusion of an
IndexSet trait.

Do you know if there's a enhancement request for IndexSet trait? Or a RFC.

@TimNN TimNN added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Oct 24, 2017
@est31
Copy link
Member Author

est31 commented Oct 24, 2017

@leonardo-m there are multiple discussions about IndexAssign, but I don't want to change the language, I just want the compiler output to be nicer.

@estebank
Copy link
Contributor

CC #52788

@est31 est31 closed this as completed Oct 19, 2018
@rust-lang rust-lang locked and limited conversation to collaborators Oct 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

4 participants