枚举数组,可能包含引用

时间:2019-03-14 11:36:24

标签: arrays enums rust

我尝试编码战舰的纸质游戏,其中战场是枚举数组。我找不到初始化数组的方法。

enum Tile {
    Water,
    Debris,
    Ship(Rc<Ship>),
}

fn main() {
    let mut a = [[Tile::Water; 10]; 10]; //Tile::Water doesn't implement Copy (the compiler is dumb)
    let mut b: [[Tile; 10]; 10];
    for i in 1..10 {
        for j in 1..10 {
            b[i][j] = Tile::Water;
        }
    } //use of possibly uninitialized b (I'm trying to initialize it)
}

这怎么办?我不是正在寻找游戏的另一种解决方案,这里只是一个例子。

1 个答案:

答案 0 :(得分:-1)

尝试一下:

enum Tile {
    Water,
    Debris,
    Ship(Rc<Ship>),
}

impl Default for Tile {
    fn default() -> Self {
        Tile::Water
    }
}

fn main() {
    let mut b: [[Tile; 10]; 10] = Default::default();
}