Skip to content

Incorrect closure type deduced despite move keyword #44336

Closed
@vitalyd

Description

@vitalyd
fn main() {
     let my_text = Some(String::from("my_text"));
     let runner = move || {
         if let Some(s) = my_text {
              println!("Here it is: {}", s);
         }
     };
     runner();
}

This fails to compile on 1.20 (and a few earlier versions that I tried) with:

error[E0507]: cannot move out of captured outer variable in an `Fn` closure
 --> src/main.rs:6:26
  |
2 |     let my_text = Some(String::from("My text"));
  |         ------- captured outer variable
...
6 |         if let Some(s) = my_text {
  |                     -    ^^^^^^^ cannot move out of captured outer variable in an `Fn` closure
  |                     |
  |                     hint: to prevent move, use `ref s` or `ref mut s`

But this should be capturing my_text by move and generating an FnOnce shouldn't it?

This issue was originally brought up in https://users.rust-lang.org/t/moving-option-into-a-closure-by-binding-it-in-a-pattern-does-not-work-noob-question/12721

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)C-bugCategory: This is a bug.T-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