Either instead of Result

What is the idiomatic way to have an Either, but not use Result?

Specifically - I want to chain a bunch of processing in a pipeline and “return early” in a monadic/railway-oriented sort of way, but it’s not really an Error/Success - so the naming of Left/Right works better for this…

Also, more generally, is the reason map and friends aren’t implemented as like a Functor trait because Rust doesn’t have higher kinded types?

You could define your own:

enum Either<L, R> {

But then you’d be missing all the nice combinators/methods that Result and Option provide, and you’d have to implement those yourself, too…

Thankfully, there’s a crate that has done that work for you :slight_smile:

And yes, I believe a Functor trait or similar isn’t currently expressible in Rust, but someone smarter than me would need to explain why that’s the case :stuck_out_tongue:


Option and Result have all the functor/applicative/monad methods I think (functor is map, applicative is can’t remember, monad is and_then). It’s just you can’t write code that’s abstract over them (yet). You can do duck typing with macros (provided e.g. all the bind methods are called and_then :P)

1 Like