【发布时间】:2019-03-14 11:36:24
【问题描述】:
我尝试编写战舰的纸质游戏,其中战场是一个枚举数组。我找不到初始化数组的方法。
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)
}
如何做到这一点?我不是为游戏寻找其他解决方案,这只是一个示例。
【问题讨论】:
-
“编译器很笨” 不……它不是。不让这种情况发生是有正当理由的。
-
@hellow 其中两个链接指向同一个问题。
-
@hellow 不,在这种情况下编译器真的很笨。没有充分的理由不允许
[Tile::Water; 10],事实上它最终应该被允许 (github.com/rust-lang/rust/issues/49147)。