在我的代码中,我发现match_num_works()
中的代码有一定的优雅。我想写一个String
匹配与类似的公式,但不能得到它的工作。我结束match_text_works()
这是不太优雅。如何在一个结构体中对字符串进行模式匹配
struct FooNum {
number: i32,
}
// Elegant
fn match_num_works(foo_num: &FooNum) {
match foo_num {
&FooNum { number: 1 } =>(),
_ =>(),
}
}
struct FooText {
text: String,
}
// Clunky
fn match_text_works(foo_text: &FooText) {
match foo_text {
&FooText { ref text } => {
if text == "pattern" {
} else {
}
}
}
}
// Possible?
fn match_text_fails(foo_text: &FooText) {
match foo_text {
&FooText { text: "pattern" } =>(),
_ =>(),
}
}
谢谢!那会做。 – ebaklund