Optional Results
Explore how to use Rust's Option enum to manage optional data safely and idiomatically. Understand handling exhaustive pattern matching for enums, and practice implementing functions that return optional results without errors.
We'll cover the following...
We'll cover the following...
The Loch Ness monster lives in Scotland. Dracula lives in Transylvania. But no one knows where Bigfoot or aliens live. Let’s write some code for this:
This won’t compile. We haven’t handled the cases of bigfoot or aliens. The compiler tells us this:
error[E0004]: non-exhaustive patterns: `&Bigfoot` and `&Alien` not covered
--> src/main.rs:20:15
|
2 | / enum Monster {
3 | | LochNess,
4 | | Dracula,
5 | | Bigfoot,
| | ------- not covered
6 | | Alien,
| | ...