3 ms·
Although I prefer exceptions, I like the way Rust approaches not having them. It also communicates exceptions via option types, and - by convention - functions
by parenthephobia 10y ago
Although I prefer exceptions, I like the way Rust approaches not having them.
It also communicates exceptions via option types, and - by convention - functions that can go wrong return an instance of Result<SuccessType, ErrorType> initialized either with a success value or an error value.
There are some helper methods on Result. I like "expect", which panics if an error is indicated and returns the success value otherwise. The real boilerplate removal comes via try!, which returns the result (actually it re-wraps it in a new Result) if it's an error. When used on functions that can fail inside functions which return Result, the result is that try! bubbles the exception up the stack.
The experience is very like traditional exception handling, but you always know exactly how control can flow, since try! only returns from the function it is directly inside, and can only from functions which advertise that they can fail by having Result<> as their return type.
- rabidferret 10y agoYeah, ultimately it's fundamentally no different than Go but with more sugar and ability to abstract properly. Turns out that counts for a lot.
- rabidferret 10y agoYeah, ultimately it's fundamentally no different than Go but with more sugar and ability to abstract properly. Turns out that counts for a lot.