Syntax sugar of closures

I'm trying to understand how closures are transformed under the hood into an anonymous struct capturing the environment + the Fn/FnMut/FnOnce trait implementation referring to self for that struct, and this is as far as I could go.

The problem is that while the upper part compiles with no problem, the later gives a compiler error (correctly), since the value was moved when call_once was invoked.. What am I missing? Why the closure wasn't moved when invoked?

From the move docs:

move closures may still implement Fn or FnMut, even though they capture variables by move. This is because the traits implemented by a closure type are determined by what the closure does with captured values, not how it captures them

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.