Hi! I'm trying to understand the compiler error, see some ambiguity here.

Perhaps it's just a language issue. English isn't my native.

If I run the example from the link above, I get the error:
closure is FnOnce because it moves the variable bob out of its environment.

I also tried to simulate the closure to better understand what's going on, because, in my opinion,
moving ownership of the captured variable to a variable declared within the closure body isn't so obvious.

I'm confused by the end of the error: ..out of its environment.. What does "environment" mean? I assume that in the context of a closure, it always means the scope outside the closure body.

You might think that the closure becomes [FnOnce] simply because it was declared with the move keyword
and values ​​are moved into it - that not true and example above does show this.

It seems like this message would be more precise and useful:
closure is FnOnce because it moves the variable bob out of its body/inner_state/self?

That is, this is somewhat analogous to the "moving out of self" I wanted to show above.

But even so, I wouldn't call everything the closure has taken its body.

There's the closure's actual body, and then there's the invisible structure created by the compiler.
And moving ownership of a value from this structure to, for example, a variable declared in the closure's body can prevent the closure from being Fn.

  1. Am I understanding this correctly?
  2. Could the compiler's message really have been clearer?

3 posts - 2 participants

Read full topic