Can traits be used on enum types?

后端 未结 1 1931
遥遥无期
遥遥无期 2020-12-06 18:54

I read through the trait documentation and found a neat definition for using traits on structs. Is it possible to use traits on enum types? I have seen answers

相关标签:
1条回答
  • 2020-12-06 19:15

    Can traits be used on enum types?

    Yes. In fact, you already have multiple traits defined for your enum; the traits Debug, Copy and Clone:

    #[derive(Debug, Copy, Clone)]
    pub enum SceneType
    

    The problem is that you aren't attempting to implement Playable for your enum, you are trying to implement it for one of the enum's variants. Enum variants are not types.

    As the error message tells you:

    help: you can try using the variant's enum: `SceneType`
    
    impl Playable for SceneType {
        fn play() {}
    }
    

    See also:

    • Can struct-like enums be used as types?
    • Is there a way to use existing structs as enum variants?
    0 讨论(0)
提交回复
热议问题