Rust 如果让语句生锈,如何组合?

Rust 如果让语句生锈,如何组合?,rust,Rust,我有下面的枚举,它表示2d游戏世界中的细胞 #[derive(Copy, Clone)] enum Cell { Empty, Soil, Metal, Diamond(Falling), Boulder(Falling), Player, Enemy, Exit } 其中下降也是一个枚举: #[derive(Copy, Clone)] enum Falling { True, False } 我还有第二个枚举:

我有下面的枚举,它表示2d游戏世界中的细胞

#[derive(Copy, Clone)]
enum Cell {
    Empty,
    Soil,
    Metal,
    Diamond(Falling),
    Boulder(Falling),
    Player,
    Enemy,
    Exit
}
其中下降也是一个枚举:

#[derive(Copy, Clone)]
enum Falling {
    True,
    False
}
我还有第二个枚举:

#[derive(Copy, Clone)]
enum PlayerInput {
    None,
    Up,
    Down,
    Left,
    Right
}
我想在Rust中实现以下逻辑,根据该逻辑,名为
canMove
的布尔值将被设置为true或false:

let cell; // is of type Cell
let input; // is of type PlayerInput

if cell is Player or Metal, FALSE
if cell is Boulder(_) or Diamond(_) and input is Down, FALSE
if cell is Boulder(NotFalling) and input is Up, FALSE
if cell is Boulder(_) and input is Right or Left, and oneMoreCondition(cell,input) is true, FALSE
otherwise TRUE

既然if-let语句不支持&&运算符,我就剩下match语句了,它仍然需要嵌套的if-let语句,结果代码的可读性就会降低。解决这个问题的惯用方法是什么?或者我误用了枚举。请提出更好的解决方案请

您可以使用
匹配
并将两个变量作为元组进行匹配:

#[derive(Copy, Clone)]
enum Cell {
    Empty,
    Soil,
    Metal,
    Diamond(Falling),
    Boulder(Falling),
    Player,
    Enemy,
    Exit
}

#[derive(Copy, Clone)]
enum Falling {
    True,
    False
}

#[derive(Copy, Clone)]
enum PlayerInput {
    None,
    Up,
    Down,
    Left,
    Right
}

fn oneMoreCondition(cell: Cell, input: PlayerInput) -> bool {
    // Any arbitrary logic here.
    true
}

fn canMove(cell: Cell, input: PlayerInput) -> bool {
    match (cell, input) {
        (Cell::Player, _) | (Cell::Metal, _) => false,
        (Cell::Boulder(_), PlayerInput::Down) | (Cell::Diamond(_), PlayerInput::Down) => false,
        (Cell::Boulder(Falling::False), PlayerInput::Up) => false,
        (Cell::Boulder(_), PlayerInput::Right) | (Cell::Boulder(_), PlayerInput::Left) if oneMoreCondition(cell, input) => false,
        _ => true,
    }
}

fn main() {
    println!("{}", canMove(Cell::Player, PlayerInput::Left));
    println!("{}", canMove(Cell::Boulder(Falling::True), PlayerInput::Down));
    println!("{}", canMove(Cell::Boulder(Falling::False), PlayerInput::Up));
    println!("{}", canMove(Cell::Boulder(Falling::False), PlayerInput::Right));
    println!("{}", canMove(Cell::Enemy, PlayerInput::Left));
}
输出:

false
错误的
错误的
错误的
符合事实的

您可以使用
match
并将两个变量作为元组进行匹配:

#[derive(Copy, Clone)]
enum Cell {
    Empty,
    Soil,
    Metal,
    Diamond(Falling),
    Boulder(Falling),
    Player,
    Enemy,
    Exit
}

#[derive(Copy, Clone)]
enum Falling {
    True,
    False
}

#[derive(Copy, Clone)]
enum PlayerInput {
    None,
    Up,
    Down,
    Left,
    Right
}

fn oneMoreCondition(cell: Cell, input: PlayerInput) -> bool {
    // Any arbitrary logic here.
    true
}

fn canMove(cell: Cell, input: PlayerInput) -> bool {
    match (cell, input) {
        (Cell::Player, _) | (Cell::Metal, _) => false,
        (Cell::Boulder(_), PlayerInput::Down) | (Cell::Diamond(_), PlayerInput::Down) => false,
        (Cell::Boulder(Falling::False), PlayerInput::Up) => false,
        (Cell::Boulder(_), PlayerInput::Right) | (Cell::Boulder(_), PlayerInput::Left) if oneMoreCondition(cell, input) => false,
        _ => true,
    }
}

fn main() {
    println!("{}", canMove(Cell::Player, PlayerInput::Left));
    println!("{}", canMove(Cell::Boulder(Falling::True), PlayerInput::Down));
    println!("{}", canMove(Cell::Boulder(Falling::False), PlayerInput::Up));
    println!("{}", canMove(Cell::Boulder(Falling::False), PlayerInput::Right));
    println!("{}", canMove(Cell::Enemy, PlayerInput::Left));
}
输出:

false
错误的
错误的
错误的
符合事实的

看看这是否有帮助:我已经看到了这一点,但它没有提出解决方案。我尝试了“如果let(Cell::Boulder()| Cell::Diamond(),PlayerInput::Down)=(next,self.current_input){…}”,但编译器说“或者模式语法是实验性的”而且不会编译。注意:为什么用值
True
/
False
而不是简单地使用
bool
?看看这是否有帮助:我看到了这一点,但它没有提出解决方案。我尝试了“如果let(Cell::Boulder()| Cell::Diamond(),PlayerInput::Down)=(next,self.current_input){…}”,但编译器说“或者模式语法是实验性的”而且不会编译。请注意:为什么使用值
True
/
False
而不是简单地使用
bool
?input==x不起作用,因为Right、Left、Up、Down是枚举的一部分。据我所知,在这种情况下,我不能使用if let注意,您可能还想导出
Eq
PartialEq
Debug
traits.input==x,因为Right、Left、Up、Down是枚举的一部分。据我所知,我不能使用if,让我们在这种情况下注意,您可能还想导出
Eq
PartialEq
Debug
特性。