【问题标题】:Match on field inside enum data in Rust?匹配Rust中枚举数据内的字段?
【发布时间】:2021-04-18 03:15:56
【问题描述】:

假设我有枚举:

enum Foo {
    Bar {baz: Option<Buzz> },
}


struct Buzz {}

有没有办法匹配baz 是否为None

How to match struct fields in Rust? 似乎不起作用,因为 Rust 解释

match foo {
    Foo::Bar { baz: Buzz } => {
    },
    Foo::Bar { baz: None } => {
    }
}

baz: Bar 重命名。

【问题讨论】:

标签: rust pattern-matching


【解决方案1】:

None 的反义词是Some

let foo = Foo::Bar{ baz: None };
match foo {
    Foo::Bar{ baz: Some(_) } => println!("Bar with some"),
    Foo::Bar{ baz: None } => println!("Bar with none"),
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多