Is there a way to use existing structs as enum variants?

后端 未结 2 1509
小蘑菇
小蘑菇 2020-12-02 02:19

I\'m using an enum for polymorphism, similar to the following:

enum MyType {
    Variant1 { a: i32, b: i32 },
    Variant2 { a: bool, b: bool },
}

2条回答
  •  春和景丽
    2020-12-02 02:48

    There have been multiple RFCs about making enum variants their own type:

    • RFC 1450: Types for enum variants
    • RFC 2593: Enum variant types

    Unfortunately, they have been postponed or not yet decided on as this is not considered a language priority.

    This means that right now, the way you have proposed is the only way to have types for enum variants.

提交回复
热议问题