Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Enums 为什么在模式匹配类似结构的枚举变量和字段时会出现错误?_Enums_Rust_Pattern Matching - Fatal编程技术网

Enums 为什么在模式匹配类似结构的枚举变量和字段时会出现错误?

Enums 为什么在模式匹配类似结构的枚举变量和字段时会出现错误?,enums,rust,pattern-matching,Enums,Rust,Pattern Matching,我无法消除此代码中的错误: #[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: &V

我无法消除此代码中的错误:

#[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(_) => {},
        };
    }
}
它告诉我查看
rustc的输出——解释E0532
以获取帮助,我做到了

它们显示了错误代码的示例:

在本例中,发生错误的原因是
State::Failed
有一个不匹配的字段。它应该是
State::Failed(ref msg)


在我的例子中,我正在匹配枚举的字段,因为我正在执行
OperationMode::CBC(41;
。为什么会发生此错误?

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

  • 单位

    enum A { One }
    
    match something {
        A::One => { /* Do something */ }
    }
    
  • 元组

    enum B { Two(u8, bool) }
    
    match something {
        B::Two(x, y) => { /* Do something */ }
    }
    
  • 结构

    enum C { Three { a: f64, b: String } }
    
    match something {
        C::Three { a: another_name, b } => { /* Do something */ }
    }
    
模式匹配时,必须使用与变量定义的语法相同的语法:

  • 单位

    enum A { One }
    
    match something {
        A::One => { /* Do something */ }
    }
    
  • 元组

    enum B { Two(u8, bool) }
    
    match something {
        B::Two(x, y) => { /* Do something */ }
    }
    
  • 结构

    enum C { Three { a: f64, b: String } }
    
    match something {
        C::Three { a: another_name, b } => { /* Do something */ }
    }
    
除此之外,您还可以使用各种允许忽略值的模式,例如
\uuuuu
。在这种情况下,您需要大括号和
全包:

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