The Rust documentation says not much about the |-operator for pattern matching. It only gives one example:
let x = 1;
match x {
1 | 2 => println!("one or two"),
3 => println!("three"),
_ => println!("anything"),
}
Contrary to the answer of this question, I noticed that the following compiles:
struct S(E, usize);
#[derive(Copy, Clone)]
enum E {
P(u8), Q(u8)
}
fn main() {
let x = S(E::P(1), 10);
if let S(E::P(..) | E::Q(..), _) = x {
}
let y = E::P(2);
match (y, y) {
(E::Q(..), E::P(..) | E::Q(..)) => {
}
_ => {}
}
}
In that particular question, integers were matched, not values of enum type. Integers implement the std::ops::BitOr trait, so there would be a conflict there for sub patterns.
My questions are:
- Is this a feature? Why does it compile? Why does it seem to work?
- What are the limitations of this feature? (e.g. type must not implement BitOr or similar)
- What is the feature called? What is the AST node used to implement the feature called? Even the name of the type of the AST node in the compiler implementation would be a better name to use in discussion than calling it the "
|-operator", which is very vague.