如何将Enummap传递给功能?

发布于 2025-01-22 05:49:13 字数 1262 浏览 0 评论 0 原文

目标:
要将ENUMMAP从ENM_MAP板条箱传递到函数

我的错误:

25 | fn print_board(板:& [cellstatus],celldict:& enummap<& cellstatus,& str>){
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^特征 enuumArray<& str> 未针对& CellStatus

相关代码:

use enum_map::{enum_map, EnumMap};

enum CellStatus{
    Empty,
    Cross,
    Circle,
}

fn print_board(board: &[CellStatus], celldict: & EnumMap<&CellStatus, &str>){
    for cell in board {
        println!("{}", celldict[cell]);

    }
}



fn main() {


    let cell_repr = enum_map! {
        CellStatus::Empty  => " _ ".to_string(),
        CellStatus::Cross  => " X ".to_string(),
        CellStatus::Circle => " O ".to_string(),
    };


}

背景:

我试图将简单的TIC-TAC-TOE游戏作为我的第一个Rust脚本。我还没有实施游戏的逻辑。我可以通过仅通过整数和一堆IF语句来使它起作用,但我想以更干净的方式编写它。

我猜我必须为我的枚举实施一些东西吗?但我不确定该怎么做。

完整代码: https://pastebin.com/pzwqyvr2

有问题的板条箱:

Goal:
To pass an EnumMap from the enum_map crate to a function

My error:

25 | fn print_board(board: &[CellStatus], celldict: & EnumMap<&CellStatus, &str>){
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait EnumArray<&str> is not implemented for &CellStatus

Relevant code:

use enum_map::{enum_map, EnumMap};

enum CellStatus{
    Empty,
    Cross,
    Circle,
}

fn print_board(board: &[CellStatus], celldict: & EnumMap<&CellStatus, &str>){
    for cell in board {
        println!("{}", celldict[cell]);

    }
}



fn main() {


    let cell_repr = enum_map! {
        CellStatus::Empty  => " _ ".to_string(),
        CellStatus::Cross  => " X ".to_string(),
        CellStatus::Circle => " O ".to_string(),
    };


}

Background:

Im trying to make a simple tic-tac-toe game as my first rust script. I havent implemented the logic yet of the game. I could make it work by just passing integers and a bunch of if statements but i wanted to write it in a more clean way.

Im guessing I have to implement something for my enum? but im not sure how to go about doing that.

Full code: https://pastebin.com/pZwqyvR2
Crate in question: https://docs.rs/enum-map/latest/enum_map/#

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

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

发布评论

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

评论(1

清晰传感 2025-01-29 05:49:14

根据您发布的文档,您必须衍生板条箱的性状才能在该 enum_map!上使用 enum 。唯一的修改是在您的 enum 定义之前:

#[derive(Enum)]
enum CellStatus {
  Empty,
  Cross,
  Circle,
}

另外, enum_map!的结果是 enummap&lt; cellstatus,string&gt; 在您的情况下,不是 enummap&lt;&amp; cellstatus,&amp; str&gt; ,更改为该类型应该有所帮助。它抱怨说&amp; cellstatus 没有实现某个特征,因为该性状是由 cellstatus 的衍生宏自动实现的,而不是用于&amp; amp; cellstatus 。但是,如果执行此操作,它仍然不会编译,因为 enummap 不会实现 index&lt;&amp; cellstatus&gt; ,但只有 index&lt; cellstatus&gt; (如果您问我,这有点愚蠢)。无论如何,修补此IMO的最佳方法是制作 cellstatus 复制。以下编译:

use enum_map::{enum_map, Enum, EnumMap};

#[derive(Enum, Clone, Copy)]
enum CellStatus {
    Empty,
    Cross,
    Circle,
}

fn print_board(board: &[CellStatus], celldict: &EnumMap<CellStatus, String>){
    for cell in board {
        println!("{}", celldict[*cell]);

    }
}

fn main() {
    let cell_repr = enum_map! {
        CellStatus::Empty => String::from("_"),
        CellStatus::Cross => String::from("X"),
        CellStatus::Circle => String::from("O"),
    };
}

According to the documentation you posted, you have to derive the crate's trait in order to use enum_map! on that enum. The only modification is just before your enum definition:

#[derive(Enum)]
enum CellStatus {
  Empty,
  Cross,
  Circle,
}

Also, the result of enum_map! is a EnumMap<CellStatus, String> in your case, not a EnumMap<&CellStatus, &str>, Changing into that type should help. It complains that &CellStatus does not implement a certain trait because that trait is automatically implemented by the derive macro for CellStatus, and not for &CellStatus. However, if you do this it will still not compile, because EnumMap does not implement Index<&CellStatus>, but only Index<CellStatus> (which is a bit stupid if you ask me). Anyways, the best way to patch this IMO is to make CellStatus Copy. The following compiles:

use enum_map::{enum_map, Enum, EnumMap};

#[derive(Enum, Clone, Copy)]
enum CellStatus {
    Empty,
    Cross,
    Circle,
}

fn print_board(board: &[CellStatus], celldict: &EnumMap<CellStatus, String>){
    for cell in board {
        println!("{}", celldict[*cell]);

    }
}

fn main() {
    let cell_repr = enum_map! {
        CellStatus::Empty => String::from("_"),
        CellStatus::Cross => String::from("X"),
        CellStatus::Circle => String::from("O"),
    };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文