3 ms·
Rust definitely has higher order functions, but it doesn't have higher kinded types required for certain things (like monads). You may have got these confused.
by vertex-four 10y ago
Rust definitely has higher order functions, but it doesn't have higher kinded types required for certain things (like monads). You may have got these confused.
- wyager 10y agoRust has very limited higher-order functions. True functional-style HOFs require passing around arbitrarily-sized heap closures, which doesn't work with rust's memory model. You cannot, for example, have a vector of functions in rust. You can sort of get it with boxed closures, but it's very inconvenient compared to in any of the languages I mentioned. That's why I didn't include it.
- vertex-four 10y agoBoxing things is the way to get arbitrarily-sized things in Rust. I've worked with vectors of closures - it works quite nicely.
- wyager 10y agoIt may work quite nicely by whatever standard you're used to, but it's extremely inconvenient compared to languages that actually make a habit of using HOFs. There's a reason people don't usually use HOFs in Rust outside of a few easily inlinable use cases. The standard library doesn't even have any sort of composition or application operators, because it would be too unwieldy to use.
- vertex-four 10y agoNo, the standard library doesn't contain many of the things that Haskell's does because it's not Haskell, and people haven't really been asking for them like they have a variety of other things. And the reason people don't use them widely is that there's often better designs than passing around closures - lifetimes make useful mutation of closures' environments difficult, Rust largely revolves around mutation, so it's more common to invert the control flow (have functions return a command and interpret it, possibly in a loop, rather than have them take a closure) except in certain self-contained situations. That doesn't change the existence of HOFs.
- wyager 10y agoSounds to me like you're saying the exact same thing I am, which is that HOFs are too inconvenient to be practical in Rust.