by fmap · 9y ago · 0 comments
Which one of these categories your function falls into is a decision of the compiler and might change if the borrow checker improves/regresses.
---
These things are pretty much non-issues unless you make heavy use of higher-order programming. For simple second-order functions such as map/filter/fold it's still easy to wrap your head around all of this. However, experience in Haskell has shown that higher-order functions are a very useful abstraction and if their usage is lightweight and intuitive they pop up all over the place. For instance, parser combinators frequently involve fourth-order functions. At this point you do not want to think about the implementation details that your compiler has to fill in.
At this point, I'm afraid that we will not see a lot of elegant higher-order prorgamming in Rust, because it is potentially so difficult to keep track of the ownership story. I'd be happy to be proven wrong, though. :)
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.
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.