What is the difference between two periods and underscore in this case:
let a = Some("a");
match a {
Some(_) => println!("we are in match _"),
_ => panic!(),
}
match a {
Some(..) => println!("we are in match .."),
_ => panic!(),
}
Both do compile and run, but what is the reason to prefer one before another?
..
form (or evenFoo { .. }
, which also works even for tuple structs/enum variants) is because it makes the match robust to changes in the number of arguments or whether it uses the braced or tuple style in the declaration. (Or you can prefer the_
form if you want the code to break and require review on any change to the variant.) When you have a whole bunch of variants with a variety of kinds of argument it is convenient to be able to write all the branches the same way. – Hotblooded