如何获得变体的参数?

rea*_*ebo 0 rust

拥有这些枚举

pub enum Symbol {
    X,
    O,
}

pub enum CellContent {
    Move(Symbol),
    Empty,
}
Run Code Online (Sandbox Code Playgroud)

let cell_content = CellContent::Move(Symbol::X);
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到Symbol?当然如果是变体的话Move(Symbol)

这不起作用

        if cell_0_0 == Move(a_symbol) {  
            return Some(a_symbol);
        }
Run Code Online (Sandbox Code Playgroud)

我也不能执行以下操作,因为我必须什么都不做(代码必须继续评估);并且以下内容根本无法编译!

        match cell_0_0 {
            Move(symbol) => return symbol;
            _ => // do nothing;
        }

        // code must go on to check further conditions
Run Code Online (Sandbox Code Playgroud)

| 我对 Rust 的基本语法仍然有一些问题,所以我正在制作一些基本程序

所以问题是

如何

  • 检查我的变量是否是以下变量的变体Move(Symbol)
  • 如果是,则返回符号(的副本)
  • 否则什么也不做,所以代码可以继续进行更多检查?

编辑1:完整(不工作)代码

pub fn some_one_win(&self) -> Option<Symbol> {
    let cell_0_0: CellContent = self.table[0][0];
    let cell_0_1: CellContent = self.table[0][1];
    let cell_0_2: CellContent = self.table[0][2];
    if cell_0_0 == cell_0_1 && cell_0_0 == cell_0_2 {
        match cell_0_0 {
            Move(symbol) => return symbol;
            _ => // how to 'do nothing' here ?;
        }
    }
    let cell_1_0: CellContent = self.table[1][0];
    let cell_1_1: CellContent = self.table[1][1];
    let cell_1_2: CellContent = self.table[1][2];
    if cell_1_0 == cell_1_1 && cell_1_0 == cell_1_2 {
        match cell_1_0 {
            Move(symbol) => return symbol;
            _ => // how to 'do nothing' here ?;
        }
    }
    ... and so on ..
}
Run Code Online (Sandbox Code Playgroud)

Ry-*_*Ry- 5

\n

我也不能执行以下操作,因为我必须什么都不做(代码必须继续评估);并且以下内容根本无法编译!

\n
match cell_0_0 {\n    Move(symbol) => return symbol;\n    _ => // do nothing;\n}\n
Run Code Online (Sandbox Code Playgroud)\n
\n

如果语法正确,你可以做到这一点:

\n
match cell_0_0 {\n    CellContent::Move(symbol) => {\n        return Some(symbol);\n    }\n    _ => {}  // do nothing\n}\n
Run Code Online (Sandbox Code Playgroud)\n

但正如 PitaJ 提到的,当 \xe2\x80\x99 只有一个模式 plus 时_,该if let构造通常是一种更干净的替代方案:

\n
if let CellContent::Move(symbol) = cell_0_0 {\n    return Some(symbol);\n}\n
Run Code Online (Sandbox Code Playgroud)\n