Optional Results

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:

Press + to interact
enum Monster {
LochNess,
Dracula,
Bigfoot,
Alien,
}
enum Place {
Scotland,
Transylvania,
}
impl Monster {
fn lives(&self) -> Place {
use Monster::*;
use Place::*;
match self {
LochNess => Scotland,
Dracula => Transylvania,
}
}
}
fn main() {} // dummy

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  | |    
...