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?
In this case, there is no difference.
In general, _
ignores one element (field, array element, tuple field etc.) while ..
ignores everything left (all fields except those explicitly specified etc.). But since Some
contains only one field, this has the same effect.
Here's an example where they differ:
struct Foo(u32, u32);
fn foo(v: Foo) {
match v {
Foo(..) => {}
Foo(_, _) => {}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With