此问题已经在这里有了答案:
我有以下匹配项
match it.peek() {
Some('0') | Some('1') | Some('2') | Some('3')
| Some('4') | Some('5') | Some('6') | Some('7')
| Some('8') | Some('9') => { list.push(...) }
_ => {...}
}
现在,我想在Some
的情况下编写一个简单的get元素,我确定这是行不通的,但是您会从中得到想法的:
match it.peek() {
Some('0' as digit) | Some('1' as digit) | Some('2' as digit) | Some('3' as digit)
| Some('4' as digit) | Some('5' as digit) | Some('6' as digit) | Some('7' as digit)
| Some('8' as digit) | Some('9' as digit) => { list.push(digit) }
_ => {...}
}
这可能吗?我知道我可以嵌套第二个匹配项,从数字中解包字符,但是然后我需要两次实现_
大小写,这是我想避免的。
您可以对char::is_digit
使用条件匹配:
fn main() {
let c = Some('2');
match c {
Some(c) if c.is_digit(10) => println!("Found a digit: {}", c),
_ => println!("Found another thing"),
}
}
我想出了这个
match it.peek() {
Some(c)
if *c == '0'
|| *c == '1'
|| *c == '2'
|| *c == '3'
|| *c == '4'
|| *c == '5'
|| *c == '6'
|| *c == '7'
|| *c == '8'
|| *c == '9' => {}
}
这似乎可以解决问题。仍然对更好的方式感兴趣