Skip to content

Consider adding a help message when the moved value was a FnOnce #40855

Closed
@jorendorff

Description

@jorendorff

In Programming Rust, a book I'm writing, there's this:


It doesn't happen often, but once in a great while you'll write some closure code that unintentionally uses up a value:

let dict = produce_glossary();
let debug_dump_dict = || {
    for (key, value) in dict {  // oops!
        println!("{:?} - {:?}", key, value);
    }
};

Then, when you call debug_dump_dict() more than once, you'll get an error message like this:

error[E0382]: use of moved value: `debug_dump_dict`
  --> debug_dump_dict.rs:18:5
   |
17 |     debug_dump_dict();
   |     --------------- value moved here
18 |     debug_dump_dict();
   |     ^^^^^^^^^^^^^^^ value used here after move
   |

This isn't a very helpful error message, unless you know what Rust is trying to tell you: a previous call to this closure moved it, because this closure is a FnOnce.


In reviewing the book, @brson encouraged me to file a bug about the diagnostic here. It's probably not worth doing anything, since closures aren't normally used this way. (I considered removing the passage from the book rather than file this bug.)

But what the heck. Conceivably a "help" could be added here that says "closure was moved because it only implements FnOnce".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions