在将类似结构的枚举变量与字段进行模式匹配时,为什么会出现错误? [英] Why do I get an error when pattern matching a struct-like enum variant with fields?

查看:140
本文介绍了在将类似结构的枚举变量与字段进行模式匹配时,为什么会出现错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法清除此代码上的错误:

I can't get rid of an error on this code:

#[derive(PartialEq, Copy, Clone)]
pub enum OperationMode {
    ECB,
    CBC { iv: [u8; 16] },
}

pub struct AES {
    key: Vec<u8>,
    nr: u8,
    mode: OperationMode,
}

impl AES {
    pub fn decrypt(&mut self, input: &Vec<u8>) {
        match self.mode {
            OperationMode::ECB => {},
            OperationMode::CBC(_) => {},
        };
    }
}

<$ c末尾的模式匹配$ c>解密函数给出错误:

error[E0532]: expected tuple struct/variant, found struct variant `OperationMode::CBC`
  --> src/main.rs:17:13
   |
17 |             OperationMode::CBC(_) => {},
   |             ^^^^^^^^^^^^^^^^^^ did you mean `OperationMode::CBC { /* fields */ }`?

它告诉我看看 rustc --exploin E0532 <的输出

It tells me to look at the output of rustc --explain E0532 for help, which I did.

他们显示了以下示例错误代码:

They show this example of wrong code:


enum State {
    Succeeded,
    Failed(String),
}

fn print_on_failure(state: &State) {
    match *state {
        // error: expected unit struct/variant or constant, found tuple
        //        variant `State::Failed`
        State::Failed => println!("Failed"),
        _ => ()
    }
}


在此示例中,发生错误是因为 State :: Failed 的字段不匹配。应该是 State :: Failed(ref msg)

In this example, the error occurs because State::Failed has a field which isn't matched. It should be State::Failed(ref msg).

在我的情况下,我匹配的是我的枚举,因为我正在执行 OperationMode :: CBC(_)。为什么会发生错误?

In my case I'm matching the field of my enum because I'm doing OperationMode::CBC(_). Why does the error happen?

推荐答案

枚举变量具有三种可能的语法:

Enum variants have three possible syntaxes:


  • 单位

  • unit

enum A { One }


  • 元组

  • tuple

    enum B { Two(u8, bool) }
    


  • struct

  • struct

    enum C { Three { a: f64, b: String } }
    


  • 模式匹配时,必须使用与变体定义的语法相同的语法:

    You have to use the same syntax when pattern matching as the syntax the variant was defined as:


    • 单位

    • unit

    match something {
        A::One => { /* Do something */ }
    }
    


  • 元组

  • tuple

    match something {
        B::Two(x, y) => { /* Do something */ }
    }
    


  • 结构

  • struct

    match something {
        C::Three { a: another_name, b } => { /* Do something */ }
    }
    


  • 除此之外,您可以使用各种允许忽略值的模式,例如 _ 。. 。在这种情况下,您需要花括号和 .. 包罗万象:

    Beyond that, you can use various patterns that allow ignoring a value, such as _ or ... In this case, you need curly braces and the .. catch-all:

    OperationMode::CBC { .. } => { /* Do something */ }
    

    另请参见:

    • Ignoring Values in a Pattern in The Rust Programming Language
    • Appendix B: Operators and Symbols in The Rust Programming Language
    • How to match struct fields in Rust?

    这篇关于在将类似结构的枚举变量与字段进行模式匹配时,为什么会出现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆