![]() Declare that we return an `FnMut` rather than an `Fn` // because the returned closure may modify mutable state // (and thus isn't safe to call in parallel - not relevant // in this example). For the sake of example, we’ll use a function that calculates the length of a string: Suppose we have an expensive computation we want to cache (perhaps it queries a database or does lots of CPU-bound work). ![]() I think that example is rather boring to consider because it doesn’t solve Real Engineering Problems, and because the typeclass solution - creating a wrapper type with the new ordering - is roughly the same amount of work for the programmer.) Caching a function (Pedagogical note: the canonical example for ML-style modules over typeclasses is perhaps creating a new kind of tree-set which accepts different orderings, rather than being restricted to a single ordering per type. However, with some additional features (associated types), we can simulate ML-style modules in Rust. ![]() Instead, Rust’s trait system resembles Haskell’s typeclasses.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |