Why does the following error occur if you're matching multiple things and binding the value to a name with @ via using a | (the OR symbol, i.e. 1000 | 2000, which means matches values 1000 or 2000)?
The Patterns chapter of the Rust language book suggests this should compile.
Error message:
error: variable `f` from pattern #1 is not bound in pattern #2
f @ 1000 | 2000 => println!("century detected: {}", f),
Code used (note that the third match
using 1000 | 2000
is the one that causes the error):
use std::old_io;
fn main() {
try_patterns();
}
fn try_patterns() {
let my_input = 1;
// WORKING - Match Range of Values (Bound to a Name)
match my_input {
e @ 1 ... 5 => println!("range element detected: {}", e),
_ => println!("anything"),
}
// WORKING - Match Multiple Values
match my_input {
1000 | 2000 => println!("century detected"),
_ => println!("anything"),
}
// NOT WORKING - Match Multiple Values (Bound to a Name)
match my_input {
f @ 1000 | 2000 => println!("century detected: {}", f),
_ => println!("anything"),
}
}