Back to questions
JuniorTheoryCommon
What happens when this program is built and run?
enum Dir {
Left,
Right,
}
fn main() {
for d in [Dir::Left, Dir::Right] {
match d {
Dir::Right => println!("right"),
}
}
}It does not compile
- Expecting
right, as ifmatchsilently skipped values with no arm:matchmust cover every variant, so leaving outDir::Leftis error E0004. - Expecting a panic when the loop reaches
Left: exhaustiveness is checked while building, so no program is produced at all.
- Which two ways can you change the
matchso that the program compiles? - Why is naming
Dir::Leftexplicitly better here than adding an_arm?