Closed
Description
This code:
fn main() {
let orig = vec![true];
for _val in orig {}
let _closure = || orig;
}
gives the following error:
error[E0382]: use of moved value: `orig`
--> src/main.rs:4:20
|
2 | let orig = vec![true];
| ---- move occurs because `orig` has type `std::vec::Vec<bool>`, which does not implement the `Copy` trait
3 | for _val in orig {}
| ----
| |
| value moved here
| help: consider borrowing to avoid moving into the for loop: `&||`
4 | let _closure = || orig;
| ^^ ---- use occurs due to use in closure
| |
| value used here after move
error: aborting due to previous error
Rust suggests writing &||
to avoid borrowing. This suggestion is completely wrong - it is orig
that should be borrowed (i.e. &orig
). Attempting to follow the suggestion might lead a user to write let _closure = &|| orig;
, which is still wrong.
Rust should properly suggest &orig
in this scenario.