-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Use of moved value error in the for
iterator could give a hint about borrowing
#25534
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
This would definitely help. I couldn't figure that error out until I found this post. |
Triage: no change, same output with new style. It should check wether the first move happened in a
Another case to consider is when moving after a borrow, which should also suggest borrowing instead of moving:
Also, the second error should probably avoid showing multiple borrow errors for the same span, as fixing one would also fix the other. |
When moving out of a for loop head, suggest borrowing it When encountering code like the following, suggest borrowing the for loop head to avoid moving it into the for loop pattern: ``` fn main() { let a = vec![1, 2, 3]; for i in &a { for j in a { println!("{} * {} = {}", i, j, i * j); } } } ``` Fix rust-lang#25534.
When moving out of a for loop head, suggest borrowing it When encountering code like the following, suggest borrowing the for loop head to avoid moving it into the for loop pattern: ``` fn main() { let a = vec![1, 2, 3]; for i in &a { for j in a { println!("{} * {} = {}", i, j, i * j); } } } ``` Fix #25534.
currently gives:
Of course, we all know that
for i in &a
etc. is correct here, but this is not very obvious to beginners. We may want to give a contextual hint for "use of moved value" errors among others---probably as a second note following the original move.(Feel free to make this a metabug if there are other such examples.)
The text was updated successfully, but these errors were encountered: