You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
diagnostics: suggest iter_mut() where trying to modify .iter()'ed vector elements inplace #62387
#[derive(Debug)]structA{a:i32,}implA{fndouble(&mutself){self.a += self.a}}fnmain(){letmut 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)]structA{a:i32,}implA{fndouble(&mutself){self.a += self.a}}fnmain(){letmut 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()!
use std::path::PathBuf;structContainer{things:Vec<PathBuf>,}implContainer{fnthings(&mutself) -> &[PathBuf]{&self.things}}// contains containersstructContainerContainer{contained:Vec<Container>}implContainerContainer{fncontained(&self) -> &[Container]{&self.contained}fnall_the_things(&mutself) -> &[PathBuf]{let a = &self.contained().iter().flat_map(|container| container.things()).cloned().collect::<Vec<PathBuf>>();unimplemented!();}}pubfnmain(){}
The error shown:
error[E0596]: cannot borrow `*container` as mutable, as it is behind a `&` reference
--> src/main.rs:25:62
|
25 | let a = &self.contained().iter().flat_map(|container| container.things()).cloned().collect::<Vec<PathBuf>>();
| --------- ^^^^^^^^^ `container` is a `&` reference, so the data it refers to cannot be borrowed as mutable
| |
| help: consider changing this to be a mutable reference: `&mut Container`
error: aborting due to previous error; 1 warning emitted
So I changed it to |&mut container| and was greeted with
error[E0308]: mismatched types
--> src/main.rs:25:51
|
25 | let a = &self.contained().iter().flat_map(|&mut container| container.things()).cloned().collect::<Vec<PathBuf>>();
| ^^^^^----------
| | |
| | expected due to this
| types differ in mutability
| help: did you mean `container`: `&&Container`
|
= note: expected reference `&Container`
found mutable reference `&mut _`
which 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 use iter_mut() :(
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/releaseknow 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():(Auto 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…