首页 > 解决方案 > 使用 struct 声明内联声明 Enum

问题描述

我可以有如下结构:

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

enum State {
    a, b, c,
}

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

struct Example {
    state: enum { a, b, c },
    properties: HashMap<String, String>,
}

有什么有效的语法吗?

标签: rustsyntax

解决方案


不,没有这样的语法。

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


推荐阅读