fn main() {
let foo = 32;
match foo {
0..(255) => {},
_ => {}
}
}
Playground
I feel as if this should be accepted (it is not, look at the playground link), as I have done this before with similar cases; I have a rhs of a range that is more complicated than an identifier, and needs to be an expression, so I use parenthesis around it.
On the other hand, this works just fine with for loops
fn main() {
for _ in 0..(255) {}
}
Playground
Why is the above in the match statement not accepted?
I'm on mobile so sorry for the low quality post.
I actually have a constant RHS, u8::max_value() as u64 to be exact. This should be okay in the eyes of the compiler as it still is a constant.
I imagine this will be legal as Rust's const support improves. In theory u8::max_value() is a const expression so the compiler should be able to deal with it, but maybe the parser/language syntax hasn't caught up yet?
The fundamental problem is that patterns are not expressions. Patterns are semantically the inverse of expressions: instead of constructing values, they deconstruct them. If function call-looking things were allowed, I'd expect them to be "pattern functions".
On a semi-related note: I've thought before that it would be nice to have a general-purpose const { ... } construct that forces constant evaluation of an expression. If we had that, it would be unambigous to allow that in patterns as a "compute this, then match against it" construct.