Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Errors in rust are not exceptions, they're data contained within an `Either` monad (Result)

Additionally, on top of the linked RFC being nearly 9 years old it doesn't at all indicate "we want to have exceptions".

The ? operator allows propogating the errors if they can't be immediately handled (similar to monadic `do` notation, or early returns)



Just because Rust people are in denial about it doesn't make them not exceptions. The Either/Result monad is isomorphic to checked exceptions, with any differences being pretty much just syntax.


It makes generics and higher-order functions much more elegant. Any function of `(a->t)->t`(or many other similar signatures) will automatically be able to return the correct error type if t is a Result<>.

Compare this to checked exceptions, where (even in an ideal world) you'd need a separate type parameter for the error type, plus a bunch of extra language features and syntax to make it work. And then what happens if you want to use the same function with _no_ error type?

For a concrete example, try using Java's `stream().map()` with checked exceptions.

Of course there are implementations of checked exceptions which are much closer to Result than Java's implementation, and in those cases I would agree with you.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: