When should I use std::cmp::ordering in a match block instead of using an if/else if statement? Is readability the only difference?
For example:
use std::cmp::Ordering;
fn main() {
match 2.cmp(&2) {
Ordering::Less => println!("Less than 2."),
Ordering::Greater => println!("Greater than 2."),
Ordering::Equal => println!("Equal to 2."),
}
}
vs.
fn main() {
if 1 < 2 {
println!("less than 2.");
} else if 1 > 2 {
println!("Greater than 2.");
} else if 1 == 2 {
println!("Equal to 2.");
}
}
Aucun commentaire:
Enregistrer un commentaire