A Little tips and lessons learned from people
Match Problem
Here is the original post. Basically, the pain point is that people want to use the match arm but, somehow, OP encountered either typo or non-existed matching. Rust did not alert to the OP. Therefore, it becomes very difficult to find out the problem.
#[test]
fn awful() {
enum Metal {
Iron, Gold, Silver,
}
use Metal::*;
let metal = Silver;
let is_gold = match metal {
Iron => false,
God => true, //<-typo, warning: unused variables
Awful => false, //warning: unreachable patterns
_ => false, //useless, warning: unreachable patterns
};
assert!(!is_gold); //panic: is_gold == true
}
Why
Based on the Match Arm documentation, the next non-matching item will become the so-called catch-all-patterns
. Sometimes, we use _
as the non-binding but match all. But, we can actually use a variable to represent as the match-all. Therefore, it stucks at God item which return true.
Solution
enum Foo {
A,
B,
}
fn foo(foo: Foo) {
use Foo::*;
let x = 123; // only a warning
#[deny(unused_variables)]
match foo {
A => println!("A"),
C => println!("B"), // compilation fails with error
}
}
As the doc mentioned, when we declare to catach-all, it is a binded variable to receive the match arm result. Therefore, we can use the attr called #[deny(unused_variables)]
to force MUST-use of the variable. This is an actual brilliant workaround to solve this pain point!