有没有办法在匹配中使用自定义模式,例如正则表达式或函数? [英] Is there a way to use custom patterns such as a regex or functions in a match?
问题描述
我正在用 Rust 编写一种玩具编程语言.我在 Ruby 中对解析器逻辑进行了原型设计:
I'm writing a toy programming language in Rust. I prototyped the parser logic in Ruby:
def rd_tree(chars)
loop do
case c = chars.next
when /\s/
# whitespace stuff
when "("
# open paren stuff
when ")"
# close paren stuff
else
# default stuff
end
end
end
现在我将其转换为 Rust:
And now I'm converting it to Rust:
fn rd_tree(chars: std::str::Chars) {
while let Some(c) = chars.next() {
if c.is_whitespace() {
// whitespace stuff
} else if c == '(' {
// open paren stuff
} else if c == ')' {
// close paren stuff
} else {
// default stuff
}
}
}
我使用 if, else-if 链是因为据我所知,Rust 的匹配特性仅限于解构、枚举和类型模式.有没有办法匹配正则表达式或布尔函数?如果没有,这里是否有比 if、else-if 更惯用的模式?我希望逻辑在未来有更多的分支,我希望它保持整洁.
I resorted to using an if, else-if chain because as far as I can tell, Rust's match feature is limited to destructuring, enums, and type patterns. Is there a way to match on regexes or boolean functions? If not, is there a more idiomatic pattern here than if, else-if? I expect the logic to have more branches in the future and I want it to stay neat.
推荐答案
还没有.match
模式必须由编译器可以静态验证的内容组成.
Not yet. The match
patterns must be composed of things that can be statically verified by the compiler.
但是,您可以使用匹配保护:
fn rd_tree(chars: std::str::Chars) {
while let Some(c) = chars.next() {
match c {
c if c.is_whitespace() => {}
'(' => {}
')' => {}
_ => {}
}
}
}
匹配保护允许您针对匹配的任何模式运行函数.
A match guard allows you to run a function against whatever the pattern matched.
在未来,持续评估可能得到改进,以允许调用函数代替模式:
In the future, constant evaluation may be improved to allow calling functions in place of a pattern:
#[derive(PartialEq, Eq)]
struct Foo {
f: usize,
g: usize,
}
impl Foo {
const fn repeated(x: usize) -> Self {
Foo { f: x, g: x }
}
}
fn main() {
let f = Foo { f: 0, g: 1 };
match f {
const { Foo::repeated(22) } => println!("hi"),
_ => println!("1"),
}
}
这项工作在问题 #57240 中进行了跟踪.RFC 2920const 表达式和模式"(及其跟踪 issue #76001) 也是相关的.
This work is tracked in issue #57240. RFC 2920 "const expressions and patterns" (and its tracking issue #76001) are also relevant.
虽然没有大量的努力,但对我来说,这对您的确切示例或正则表达式如何工作并不是很明显.
It's not immediately obvious to me how this would work with your exact example or a regex without a substantial amount of effort though.
这篇关于有没有办法在匹配中使用自定义模式,例如正则表达式或函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!