我正在用Rust写一个编译器。作为词法分析器的一部分,我尝试将输入流中的单个字符与一定范围内的字符(多个)进行匹配。我目前正在尝试一种使用..运算符的方法。
match input_token {
'Z'..'a' => { // I want to match any character from 'a' -> 'z' and 'A' -> 'Z' inclusive
... run some code
}
}是否可以在Rust match表达式/语句的单个臂中匹配多个值?
发布于 2020-02-07 04:36:12
包含范围的模式是begin..=end,您可以使用|组合模式,因此您需要
match input_token {
'A'..='Z' | 'a'..='z' => {
... run some code
}
}发布于 2020-05-23 11:53:19
另一种方法是使用if模式来处理更复杂的情况:
match input_token {
token if token.is_ascii_alphabetic() {
// ...
}
// ...
}https://stackoverflow.com/questions/60102442
复制相似问题