Rust可以匹配struct字段吗?例如,这段代码:
struct Point {
x: bool,
y: bool,
}
let point = Point { x: false, y: true };
match point {
point.x => println!("x is true"),
point.y => println!("y is true"),
}
Run Code Online (Sandbox Code Playgroud)
应该导致:
y is true
Run Code Online (Sandbox Code Playgroud) 我是Rust初学者,我无法解决这类问题.我曾尝试更换&name用name,但是"格局的错误&_没有覆盖"的发生.
fn get_project(name: &'static str) {
match &name {
"hi" => {},
}
}
fn main() {
let project = get_project("hi");
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
error[E0308]: mismatched types
--> <anon>:3:9
|
3 | "hi" => {},
| ^^^^ expected &str, found str
|
= note: expected type `&&str`
= note: found type `&'static str`
Run Code Online (Sandbox Code Playgroud) rust ×2