小编Ell*_*aha的帖子

Rust 有办法将字符串类型限制为特定值(字符串文字类型)吗?

在 Typescript 中,有多种方法可以输入值,因此它只能是特定的字符串

例如

type example = "hello" | "world";
Run Code Online (Sandbox Code Playgroud)

我是否仅限于 Rust 中的“String”类型,或者有没有办法像 Typescript 中那样缩小范围?

现在我有:

pub struct ButtonProps {
    pub color: String
}
Run Code Online (Sandbox Code Playgroud)

但颜色可以是任何东西,我只想它是“主要”或“次要”

我该怎么做呢?

rust

1
推荐指数
1
解决办法
302
查看次数

标签 统计

rust ×1