小编Ant*_*yde的帖子

当模式匹配类似结构的枚举变体与字段时,为什么会出现错误?

我无法摆脱这段代码的错误:

#[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(_) => {},
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

decrypt函数末尾的模式匹配会产生错误:

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 */ }`?
Run Code Online (Sandbox Code Playgroud)

它告诉我看看rustc …

enums pattern-matching rust

5
推荐指数
1
解决办法
1597
查看次数

标签 统计

enums ×1

pattern-matching ×1

rust ×1