let some_stuff: ST = ...;
let some_other_stuff: SOT = ...;
let closure = |some_argument: SA| {
use(some_stuff, some_other_stuff)
};
closure(some_argument);
is the same as:
let some_stuff: SS = ...;
let some_other_stuff: SOS = ...;
struct Closure { // defined at compile-time
some_stuff: [&_] SS,
some_other_stuff: [&_] SOS,
}
impl Closure { // defined at compile-time
fn call([&_] self, some_argument: SA) -> ...
{
use(self.some_stuff, self.some_other_stuff)
}
}
let closure = Closure { // created at runtime
some_stuff: [&_] some_stuff,
some_other_stuff: [&_] some_other_stuff,
}
closure.call(some_argument);
Ergo, creating a closure == creating a struct with the captured environment, and calling a closure == calling a method of such struct.
See @RustyYato's blog post about closures' sugar for a more detailed description of this closure / object equivalence.