【问题标题】:How do I check if something is a type of enum without bothering about the associated value?如何在不关心关联值的情况下检查某物是否是枚举类型?
【发布时间】:2022-12-10 17:22:09
【问题描述】:

我有:

enum Instruction {
    Noop,
    Addx(isize),
}

然后这个:

if self.current_instruction == Instruction::Addx {}

不起作用。为什么?我不关心 Addx 中有什么,我只想在当前指令是 Addx 时做一些事情。

【问题讨论】:

    标签: rust discriminated-union


    【解决方案1】:

    结果证明这是有效的:

    if let Instruction::Addx(_) = self.current_instruction {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 2021-11-01
      • 2020-09-15
      • 2015-02-12
      • 1970-01-01
      • 2013-04-12
      相关资源
      最近更新 更多