Skip to content

"cannot move out of captured outer variable in an FnMut closure" should mention the outer variable #31752

Closed
@Manishearth

Description

@Manishearth
use std::thread;

fn main() {
    let things = vec![1,2,3,4];
    let mut buf = vec![];
    let handle_thread: Vec<_> = things.iter().map(|&d| {
        thread::spawn(move || {
            do_thing(&mut buf);
        })
    }).collect();
}

fn do_thing(x: &mut [u8]) {}

(https://gist.github.com/772bde36b6cddebcd46a)

gives

<anon>:11:17: 13:4 error: cannot move out of captured outer variable in an `FnMut` closure [E0507]
<anon>:11       thread::spawn(move || {
<anon>:12           do_thing(&mut buf);
<anon>:13       })
error: aborting due to previous error

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions