3 ms·
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 ac
by joshmarlow 7y ago
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.