3 ms·
> Internally, functions are implemented as closures which might capture ownership This seems backwards. Closures are implemented as a struct for the environmen
by steveklabnik 10y ago
> Internally, functions are implemented as closures which might capture ownership
This seems backwards. Closures are implemented as a struct for the environment, plus a method on that struct that represents the function call.
This then ends up the exact same way as any other function or method call in Rust: with taking self by value, by reference, or by mutable reference.
- fmap 10y agoThe environment contains references/copies/borrows of local variables, depending on a number of conditions on the code. Since the environment struct is generated by the compiler, this is different from a method call where you specify the struct yourself.
- steveklabnik 10y agoIt's still fundamentally sugar; on nightly, you could write it all yourself.
- fmap 10y agoWhat exactly are you arguing for? Closures can be implemented using closure conversion as a derived form in most languages. That's how you compile the code in the first place... The point is that you reason about closures as ordinary functions with local assumptions. You don't need to know how they are implemented at all. On the other hand, in Rust you need to be aware of the implementation in order to use higher-order functions effectively.
- steveklabnik 10y agoI'm arguing that the statement I quoted in my above comment is incorrect; closures are implemented with functions, not the other way around.