Monad Interface: Rust Edition
Implementing Monads in Rust
I’ve done a lot of writing about functional programming. But mostly in JavaScript. I love the simplicity of declarations that are possible thanks to JavaScript’s dynamic typing system. It makes defining generic things very easy — a concept that is much more complicated in strongly typed languages. One generic concept that I love expressing in JS is the functional keystone known as the Monad. A Monad is an encapsulation of an associative binary operation. In other words, you can call map
on it with an appropriate function parameter to change the inner value — even its type. It wraps some value and and allows us to adhere to a simple interface to act on that value. So how can I express this highly generic concept in Rust? Is it even possible?
I assume, reader, that you have some interest in functional programming. You may even already know what a Monad is, and simply want to know how to implement them in Rust. If this is you, read on! You’ve come to the right place.
Recently, I was looking for examples of good implementations of Monad in Rust and I came across this article. Its all about using generic associated types in traits. I was reading along, eagerly eating up the information. This is beautiful, I thought, this is amazing, this— isn’t currently possible in Rust. Unfortunately, at the time of…