如何将这个匹配语句转换为 HashMap?

发布于 2025-01-16 05:35:15 字数 367 浏览 2 评论 0原文

如何将这个 match 块转换为 HashMap?理想情况下,我想从清单文件创建 HashMap:

match layers[1][p] {
    ',' => tile = grass,
    '*' => tile = sand,

    // And so on....
}

我之前尝试这样做,但 Macroquad 的 Texture2D 与 HashMap 不兼容。 (我认为编译器说“不能将选项用作Texture2D”。)

我想做这样的事情:

let tile = hashmap.get(layers[p]);
blit(tile);

How could I convert this match block into a HashMap? Ideally I would like to create a HashMap from a manifest file:

match layers[1][p] {
    ',' => tile = grass,
    '*' => tile = sand,

    // And so on....
}

I tried doing this earlier, but Macroquad's Texture2D is not compatible with a HashMap. (I think the compiler said 'Cannot use Option as Texture2D'.)

I would like to do something like this:

let tile = hashmap.get(layers[p]);
blit(tile);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2025-01-23 05:35:15

让我们保持简单。我不认为 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)
}

Let's keep this simple. I don't think hashmap will simplify anything but why not demonstrate it. The true advantage of hash is that you can load tiles dynamically and make them loaded into the map. For now, though, we will hardcode some.


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)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文