News for hackers. Stuff that matters.
Re:Things I Enjoy in Rust: Error Handling by anuragsoni on Monday April 22, 2019 @04:40PM (#19722752)
While its true that one can use `unwrap`, pattern matching and other higher order methods available for the Result type are a more idiomatic way to work with these.

For example:

    match <something that returns result> {
        Ok (v) => <do something with v>,
        Err (some error) => <handle error>
    }
By using the result type, the compiler ensures that the only way to get a value out of the result type is via pattern matching. So the errors are still values, except there is no need to come up with a default value for the error scenario, and there is no need to come up with an "empty" value.

In isolation it might look verbose, but rust also provides a lot of higher order operations that one can do on result types. Ex:

    myResult.map(<do-something>).and_then(sq).or_else(<do-something-else>);
Something else that I personally like about a result type is that it also serves as documentation by clearly marking operations that can fail, and have a consistent way for the end-user to know which value indicates that the response is a success or an error.

EDIT: Formatting

There are no comments on this comment yet.