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 */ }
另见: