Closed
Description
Given:
use std::collections::HashMap;
struct Test {
v: u32,
}
fn main() {
let mut map = HashMap::new();
map.insert("a", Test { v: 0 });
for (k, mut v) in map.iter() {
v.v += 1;
}
}
we emit:
error[E0594]: cannot assign to `v.v` which is behind a `&` reference
--> src/main.rs:12:9
|
11 | for (k, mut v) in map.iter() {
| ----- help: consider changing this to be a mutable reference: `&mut Test`
12 | v.v += 1;
| ^^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written
but we should be instead suggesting
error[E0594]: cannot assign to `v.v` which is behind a `&` reference
--> src/main.rs:12:9
|
11 | for (k, mut v) in map.iter() {
| ----- ---- help: consider calling `iter_mut` instead
| |
| this binding is of type `&Test`
12 | v.v += 1;
| ^^^^^^^^ `v` is a `&` reference, so the data it refers to cannot be written
CC #49839
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: Suggestions generated by the compiler applied by `cargo fix`Diagnostics: Confusing error or lint that should be reworked.Diagnostics: A structured suggestion resulting in incorrect code.Diagnostics: Confusing error or lint; hard to understand for new users.Relevant to the compiler team, which will review and decide on the PR/issue.