Hi.
In a match arm, if we want to use the value we are testing for pattern matching, one currently needs to use the @ operator to bind it to a new variable. Could the compiler automatically create a new variable with the same name as the identifier we are matching against?
Let's consider this example from The book:
enum Message {
Hello { id: i32 },
}
let msg = Message::Hello { id: 5 };
match msg {
Message::Hello { id: id_variable @ 3..=7 } => println!("Found an id in range: {}", id_variable),
Message::Hello { id: 10..=12 } => println!("Found an id in another range"),
Message::Hello { id } => println!("Found some other id: {}", id),
}
Would it be possible/preferable to be able to rewrite the first arm as follows?
Message::Hello { id: 3..=7 } => println!("Found an id in range: {}", id),
The compiler could then automatically expand it to
Message::Hello { id: id @ 3..=7 } => println!("Found an id in range: {}", id),
From my understanding, the compiler already creates a variable with the same name as an identifier in the third arm, so I do not see any fundamental reason why we couldn't do it as well for the first arm.