-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"cannot move out of captured outer variable in an FnMut
closure" should mention the outer variable
#31752
Comments
I've ran into this error, until this issue is closed maybe it would be good to have an example of a fix here. Thanks! |
The fix is to figure out which captured variable is being moved and not move it. In this case, the variable |
Perfect, thanks! On Tue, Apr 19, 2016 at 8:53 AM, Manish Goregaokar <notifications@github.com
|
I've ran into this too. let mut tmp = 1;
let c = &mut tmp;
vec![0;10].iter().flat_map(|&a| {
(0..10).map(move |b|{
a + b + *c
})
});
The error does not say that the problematic variable is In real code, with multiple variables used and type inference hiding the details, it is really hard to figure out what this error is about. |
Yet another example of this (simplified from an actual use-case I ran into): fn main() {
use std::thread;
use std::sync::mpsc;
let (tx, rx) = mpsc::channel();
let n = 10;
let foos: Vec<_> = (0..n)
.into_iter()
.map(|i| thread::spawn(move || -> bool { rx.recv().unwrap() }))
.collect();
} |
The compiler is a little better at this now that it highlights which variable is the problem, but I think we could still do better. Consider fn foo(mut f: Box<FnMut()>) {
f();
}
fn main() {
let x = vec![];
x.push(true);
let y = true;
foo(Box::new(move || y = false) as Box<_>);
}
For |
Point at variable moved by closure Fix rust-lang#41482, rust-lang#31752.
Point at variable moved by closure Fix rust-lang#41482, rust-lang#31752.
Point at variable moved by closure Fix rust-lang#41482, rust-lang#31752.
I believe this was fixed in PR #41523 |
Agreed, closing. @jonhoo notes that the diagnostic for assignment isn't quite as good, but as shown below we still highlight the span for the variable assignment which doesn't work, so I suspect it's fairly clear which variable can't be assigned to. If you disagree, please open a new tracking issue for that. fn foo(mut f: Box<FnMut()>) {
f();
}
fn main() {
let y = true;
foo(Box::new(move || y = false) as Box<_>);
}
|
Hmm, I still think it'd be better if the error was:
to bring it in line with the non-closure case... |
Opened #41790 to track the assignment case. |
(https://gist.github.com/772bde36b6cddebcd46a)
gives
Except that it's totally confusing as to which outer variable is being talked about. In this case it's obvious since there's a single capture, but this won't always be the case.
The error should at least mention the name of the captured variable, and preferably point out where the variable was captured and how to fix it.
The text was updated successfully, but these errors were encountered: