-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
code:
#[derive(Debug)]
struct A {
a: i32,
}
impl A {
fn double(&mut self) {
self.a += self.a
}
}
fn main() {
let mut v = [A { a: 4 }];
v.iter().for_each(|a| a.double());
println!("{:?}", v);
}
The code gives the following not-very-helpful warning:
error[E0596]: cannot borrow `*a` as mutable, as it is behind a `&` reference
--> src/main.rs:14:27
|
14 | v.iter().for_each(|a| a.double());
| - ^ `a` is a `&` reference, so the data it refers to cannot be borrowed as mutable
| |
| help: consider changing this to be a mutable reference: `&mut A`
warning: variable does not need to be mutable
--> src/main.rs:13:9
|
13 | let mut v = [A { a: 4 }];
| ----^
| |
| help: remove this `mut`
|
= note: #[warn(unused_mut)] on by default
error: aborting due to previous error
trying to use
v.iter().for_each(|&mut a| a.double());
and a none-mutable vector just causes more errors
error[E0308]: mismatched types
--> src/main.rs:14:24
|
14 | v.iter().for_each(|&mut a| a.double());
| ^^^^^^ types differ in mutability
|
= note: expected type `&A`
found type `&mut _`
= help: did you mean `mut a: &&A`?
The actual fix is to use iter_mut()
instead of just iter()
:
#[derive(Debug)]
struct A {
a: i32,
}
impl A {
fn double(&mut self) {
self.a += self.a
}
}
fn main() {
let mut v = [A { a: 4 }];
v.iter_mut().for_each(|a| a.double());
println!("{:?}", v);
}
It would be very helpful the compiler could suggest iter_mut()
!
edmorley
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
matthiaskrgr commentedon Jul 4, 2019
@rustbot modify labels: A-diagnostics
rustbot commentedon Jul 4, 2019
Error: Parsing label command in comment failed: ...ify labels|error: must have : or to as label starter at >| A-diagnos...
Please let
@rust-lang/release
know if you're having trouble with this bot.matthiaskrgr commentedon Aug 28, 2020
Urgh I just ran into this AGAIN. 😭
My code was something like this:
The error shown:
So I changed it to
|&mut container|
and was greeted withwhich made me wonder if I was better off with the origin
|container|
.I really wish rust could somehow detect the
.iter().bla(|x| fn_that_requires_mut(x)
pattern and suggest to useiter_mut()
:(.iter()
should be made mutable #94060Auto merge of rust-lang#115308 - chenyukang:yukang-fix-62387-iter-mut…
Rollup merge of rust-lang#115308 - chenyukang:yukang-fix-62387-iter-m…
Auto merge of #115308 - chenyukang:yukang-fix-62387-iter-mut, r=david…