拥有这些枚举
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)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)
\n\n我也不能执行以下操作,因为我必须什么都不做(代码必须继续评估);并且以下内容根本无法编译!
\nRun Code Online (Sandbox Code Playgroud)\nmatch cell_0_0 {\n Move(symbol) => return symbol;\n _ => // do nothing;\n}\n
如果语法正确,你可以做到这一点:
\nmatch cell_0_0 {\n CellContent::Move(symbol) => {\n return Some(symbol);\n }\n _ => {} // do nothing\n}\nRun Code Online (Sandbox Code Playgroud)\n但正如 PitaJ 提到的,当 \xe2\x80\x99 只有一个模式 plus 时_,该if let构造通常是一种更干净的替代方案:
if let CellContent::Move(symbol) = cell_0_0 {\n return Some(symbol);\n}\nRun Code Online (Sandbox Code Playgroud)\n