我如何将这个匹配语句转换成一个 HashMap?
How can i convert this match statement into a HashMap?
如何将这个 match
块转换为 HashMap?理想情况下,我想从清单文件创建一个 HashMap:
match layers[1][p] {
',' => tile = grass,
'*' => tile = sand,
// And so on....
}
我之前试过这样做,但是 Macroquad 的 Texture2D 与 HashMap 不兼容。 (我认为编译器说 'Cannot use Option as Texture2D'。)
我想做这样的事情:
let tile = hashmap.get(layers[p]);
blit(tile);
让我们保持简单。我不认为 hashmap 会简化任何事情,但为什么不演示它。散列的真正优点是您可以动态加载瓦片并使它们加载到地图中。不过现在,我们将硬编码一些。
fn main() {
/* snip */
let mut textures = HashMap::new();
textures.insert(',', grass);
textures.insert('*', sand);
/* snip */
// mind that your program will crash if map does
// not contain the texture
let texture = textures.get(layers[p]).unwrap();
blit(texture)
}
如何将这个 match
块转换为 HashMap?理想情况下,我想从清单文件创建一个 HashMap:
match layers[1][p] {
',' => tile = grass,
'*' => tile = sand,
// And so on....
}
我之前试过这样做,但是 Macroquad 的 Texture2D 与 HashMap 不兼容。 (我认为编译器说 'Cannot use Option as Texture2D'。)
我想做这样的事情:
let tile = hashmap.get(layers[p]);
blit(tile);
让我们保持简单。我不认为 hashmap 会简化任何事情,但为什么不演示它。散列的真正优点是您可以动态加载瓦片并使它们加载到地图中。不过现在,我们将硬编码一些。
fn main() {
/* snip */
let mut textures = HashMap::new();
textures.insert(',', grass);
textures.insert('*', sand);
/* snip */
// mind that your program will crash if map does
// not contain the texture
let texture = textures.get(layers[p]).unwrap();
blit(texture)
}