简体   繁体   English

如何忽略模式匹配中类似结构的枚举变体的成员?

[英]How to ignore a member of a struct-like enum variant in pattern matching?

How do I remove the unused_variables warning from the following code? 如何从以下代码中删除unused_variables警告?

pub enum Foo {
    Bar {
        a: i32,
        b: i32,
        c: i32,
    },
    Baz,
}

fn main() {
    let myfoo = Foo::Bar { a: 1, b: 2, c: 3 };
    let x: i32 = match myfoo {
        Foo::Bar { a, b, c } => b * b,
        Foo::Baz => -1,
    };
    assert_eq!(x, 4);
}

I know I can ignore struct members after a certain point with: 我知道我可以在某一点之后忽略struct成员:

Foo::Bar { a, .. } => // do stuff with 'a'

But I can't find documentation anywhere that explains how to ignore individual struct members. 但我无法在任何地方找到解释如何忽略单个struct成员的文档。

Code on Rust Playground 关于Rust Playground的代码

I know I can ignore struct members after a certain point with: 我知道我可以在某一点之后忽略struct成员:

The .. is not positional. ..不是位置的。 It just means "all the other fields": 它只是意味着“所有其他领域”:

Foo::Bar { b, .. } => b * b,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在将类似结构的枚举变量与字段进行模式匹配时会出现错误? - Why do I get an error when pattern matching a struct-like enum variant with fields? 类似结构的枚举变量中的更新字段 - Update field in struct-like enum variant 类似结构的枚举可以用作类型吗? - Can struct-like enums be used as types? 如何仅序列化变量的名称并忽略结构中枚举字段的值(serde) - How to serialise only name of variant and ignore value for enum field in struct (serde) Rust - 引用结构的枚举变体 - Rust - Enum variant that references a struct 有没有办法在没有模式匹配的情况下直接访问枚举结构中的字段值? - Is there a way to directly access a field value in an enum struct without pattern matching? 在宏中匹配类似元组的枚举变体,其中枚举类型和变体是元变量:如何编写匹配模式? - Matching tuple-like enum variants in a macro, where enum type and variants are metavariables: how do I write the matching pattern? 自定义模式匹配失败,因为“Enum case is not a member of type” - Custom pattern matching fails with `Enum case is not a member of type` 如何在没有模式匹配的情况下比较枚举 - How to compare enum without pattern matching 结构中的枚举数组打印变体名称和值 - Array of enum in a struct prints variant names and value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM