Confusing compiller error message when [`FnOnce`] closure moves captured ownership-value out of self

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?

it means the context "captured" by the closure.

you can think of it as the (unnamable) Self type of the of the closure.

when it comes to ownerships, the move keyword means the values are captured by value, and the FnOnce trait means the captured environment can be consumed/dropped, a.k.a. being "moved out". they are related but not the same.

here's a great summary of the concepts in previous discussions:

Thank you for your quick response!

It seems to be more of a linguistic confusion for me personally.

However, the term "captured context", or even unnamable Self which you suggested, in the compiler's message, would better describe what's happening.

The environment is much more ambiguous.

actually, "env" or "environment" is rather standard terminology in programming languages. sometimes it is interchagable with "context", when describing closures, but "context" is also used more broadly, while "env" is specific to lambda expressions, a.k.a. closures.

theoretically, an "environment" is just a list of variable bindings that could appear in the lambda definition. in text books and literatures, the environment is usually denoted as E, env, or the greek letter ρ. technically, "environment" refers to the enclosing scope where the lambda expression is defined, but for the purpose of type checking the closure, we only cares the captured environment, i.e. the Self type in rust.

for example, in chapter 10.4 "Environment Model" of the book "OCaml Programming: Correct, Efficient, Beautiful":

In this model, there is a data structure called the dynamic environment, or just “environment” for short, that is a dictionary mapping variable names to values.

btw, this book is freely available online, with accompanying video lectures from Cornell CS 3310.

here's another quote from an older but classic text book, SICP, section 3.2, "The Environment Model of Evaluation":

An environment is a sequence of frames. Each frame is a table (possibly
empty) of bindings, which associate variable names with their corresponding
values.

it's totally fine for people to get confused by terminology, especially when the it's a common word in everyday language. people come to programming from various background, not everyone has a CS degree or studied CS in college.

Are you trying to argue that a word isn't Ambiguous by pointing out its multiple possible meanings?

Ha-ha. Perhaps I expressed myself incorrectly. And I was specifically referring to the CS language only..

The compiler's description of why a closure only implements FnOnce was ambiguous to me, not "environemnt" term, so:

  • because it moves ownership from outside - from the environment - which is incorrect
  • or because it moves ownership from the closure's environment (the compiler-generated structure).

So why not call it the closure's internal state or Self or something else?

That is, there is no ambiguity of the "environment", and what is meant is the outer scope surrounding the closure and the list of bindings defined in it.

Otherwise, I can translate the compiler's statement: closure is FnOnce because it moves the variable bob out of its environment as "consumes ownership from the outer environment into the captured closure context" (also called the environment).

Perhaps this option isn't even considered by native speakers?

What is literally on the outside and what is literally on the inside are both referred to by the same word - "environment" - seems strange for me!)

no, I just explain the word has a well established meaning in this specific context, but it can feel ambiguous depending on peoples background, and that's normal.

it's the latter[1], it's FnOnce because the code moves value out of the Self type. in the playground example specifically, the variable bob is moved (into the local variable _name);

    let bob = "alice".to_string();

    // uncomment closure to see error

    let closure = move || {

         let _name = bob;
    };

the desugared the code looks like this:

struct ClosureType {
    bob: String,
}
/// for illustration only, this is NOT valid in rust
impl FnOnce() for ClosureType {
    fn call_once(self) {
        // this must use `self`, not `&self` or `&mut self`
        let _name = self.bob;
    }
}

yeah, such nuaunces are often overlooked because once people get used to the terms, it just feels "natural" and they don't think about it any more.

EDIT:

I just want to add, -- and this is not a dispute in any way -- from the perspective of the type checker, it is NOT necessary to differentiate the "inside" and "outside" of the closure, everything works with the environment. in theory, all we need is rules to deduce the types of variables, be it T, &T, or &mut T, then it can be checked as if it were just another lexical scope.

however, it is a very useful tool, especially for people with strong C/C++ background, so we often use the desugared form to explain the Fn*() traits and how closures work in rust.

we often informally mention a Self type refering the type of the closure in such context, but formally, the variables are stored in a (captured) snapshot of the environment where the closure was defined, there's no such Self type.

in other words, how closures are desugared/transformed/lowered is an implementation detail of the compiler, and is orthogonal to how they are specified and type-checked.

nevertheless, rust does care about the quality of error messages, closures are special cased in the type checker anyway, so that we can have closure specific error messages instead of some generic type errors.

if you have any suggestion to make the error message clears and less ambiguous, feel free to follow up with your perspective and ideas. or better yet, you can open a topic on the internals forum where changes to the languages and compiler are proposed and discussed, including error messages.


  1. EDIT: although the former point is also relevant, namely, the move keyword makes the identifier bob to have the String type, which is not Copy, so the closure must be FnOnce. if move were left out, the identifier bob will have the &String type, which IS Copy, so the closure can be Fn, but you'll get an different error related to lifetime, because you cannot return a closure that captured a local variable by reference ↩︎