使用结构体声明内联声明 Enum

3 syntax rust

我可以有如下结构:

struct Example {
    state: State,
    properties: HashMap<String, String>,
}

enum State {
    a, b, c,
}
Run Code Online (Sandbox Code Playgroud)

如果示例结构是 State 枚举的唯一用户,我认为在结构中声明它是有意义的:

struct Example {
    state: enum { a, b, c },
    properties: HashMap<String, String>,
}
Run Code Online (Sandbox Code Playgroud)

有没有有效的语法?

mca*_*ton 5

不,没有这样的语法。

你可以检查s的 Rust 语法struct,字段的类型必须是类型表达式。类型表达式不能创建新类型。