News for hackers. Stuff that matters.
Re:Things I Enjoy in Rust: Error Handling by joshmarlow on Monday April 22, 2019 @04:21PM (#19722533)
You can do `.unwrap()` or `.expect(..)` to get your values, and in that respect, it's not that different from working in a language with `null`.

But you can actually almost totally avoid calling `unwrap`/`expect` in Rust code by doing pattern matching.

` match x { Some(value) => <DO SOMETHING WITH VALUE> None => <HANDLE THIS ERROR PATH>, }`.

If you always do that, then you've got the compiler reminding you to handle the case where it is `null`. That means that you never hit the equivalent of a null exception.

That being said, I still find myself doing some `unwrap` and `expect` (particularly in tests). But relying on matching most of the time as a first line of defense will prevent the equivalent of a null exception in the bulk of your code.

There are no comments on this comment yet.