【发布时间】:2019-01-05 03:11:33
【问题描述】:
我通读了trait documentation 并找到了在结构上使用特征的简洁定义。是否可以在 enum 类型上使用特征?我看到回答说不,但他们已经 3 岁了,并没有完全按照我的意愿去做。
我尝试过这样做:
#[derive(Debug, Copy, Clone)]
pub enum SceneType {
Cutscene,
Game,
Menu,
Pause,
Credits,
Exit,
}
//We want to guarantee every SceneType can be played statically
trait Playable {
fn play();
}
impl Playable for SceneType::Cutscene {
fn play() {}
}
error[E0573]: expected type, found variant `SceneType::Cutscene`
--> src/main.rs:16:19
|
16 | impl Playable for SceneType::Cutscene {
| ^^^^^^^^^^^^^^^^^^^
| |
| not a type
| help: you can try using the variant's enum: `SceneType`
我不明白这个错误,因为它引用的枚举在同一个文件中。如果我真的不能在枚举变体上使用特征,有什么办法可以保证任何枚举特征都必须实现某些方法?
【问题讨论】: