5 ms·
Rust do still has exceptions, they're just called "results". You can't really have values as exceptions unless you plug a lot of ad-hoc constructs into your lan
by hirrolot 4y ago
Rust do still has exceptions, they're just called "results". You can't really have values as exceptions unless you plug a lot of ad-hoc constructs into your language so that they eventually become similar to exceptions [1]:
> Add syntactic sugar for working with the Result type which models common exception handling constructs.
The whole error handling story with Rust can be summarized as "we have results but want to have exceptions".
[1] https://rust-lang.github.io/rfcs/0243-trait-based-exception-handling.html https://rust-lang.github.io/rfcs/0243-trait-based-exception-...
- dsQTbR7Y5mRHnZv 4y agoErrors 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)
- pwdisswordfish9 4y agoJust 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.
- jcparkyn 4y agoIt 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.
- cornstalks 4y agoNo! Exceptions have a very well defined meaning. Exceptions in Rust are non-aborting panics.
- kibwen 4y agoWhen people talk about "exceptions", they mean resumable exceptions that are intended to be caught. But "catching" an unwinding panic in Rust is so (deliberately!) limited, cumbersome, and unidiomatic that it doesn't qualify as an implementation of resumable exceptions. The only reason the ability to "catch" an unwinding panic even exists in Rust is to prevent unwinding across FFI boundaries, which would be UB; it's a correctness mechanism, not an error-handling mechanism.