小编mak*_*aku的帖子

如何匹配Rust中的struct字段?

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

10
推荐指数
2
解决办法
5396
查看次数

如何匹配Rust中的&'static str

我是Rust初学者,我无法解决这类问题.我曾尝试更换&namename,但是"格局的错误&_没有覆盖"的发生.

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

3
推荐指数
1
解决办法
846
查看次数

标签 统计

rust ×2