match
statements with only one useful arm can be reduced to an if-else
statement.
This helps improve readability, an if-else
statement nests lesser than a match
.
Use the pattern of the single match arm as the pattern within an if let
statement.
The body of the wildcard arm resides within the else
block.
let v = 5;
match v {
3..10 => {
println!("The range 3..10 contains v!");
},
_ => {
println!("v is outside the range 3..10");
},
}
let v = 5;
if let 3..10 = v {
println!("The range 3..10 contains v!");
} else {
println!("v is outside the range 3..10");
}