|
|
|
|
|
by gpm
2339 days ago
|
|
I feel like the "canonical" example of using pattern matching for nested if statements is fizzbuzz fn main() {
for i in 1..102 {
match (i % 3 == 0, i % 5 == 0) {
(true, true) => println!("FizzBuzz"),
(true, false) => println!("Fizz"),
(false, true) => println!("Buzz"),
(false, false) => println!("{}", i)
}
}
}
Not only is it obviously correct, but if you somehow manage to forget a case the compiler will tell you. |
|