Answer#
match checks at compile time that no case can slip through, which is the reason enums are safe to extend: add a variant and every match on it that is not updated stops compiling.
enum Light {
Red,
Yellow,
Green,
}
fn name(l: Light) -> &'static str {
match l {
Light::Red => "stop",
Light::Green => "go",
}
}
fn main() {
println!("{}", name(Light::Yellow));
}error[E0004]: non-exhaustive patterns: `Light::Yellow` not covered
--> src/main.rs:8:11
|
8 | match l {
| ^ pattern `Light::Yellow` not covered
|
note: `Light` defined here
--> src/main.rs:1:6
|
1 | enum Light {
| ^^^^^
2 | Red,
3 | Yellow,
| ------ not covered
= note: the matched value is of type `Light`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
|
10~ Light::Green => "go",
11~ Light::Yellow => todo!(),
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0004`.The answers below are the two valid fixes.