How to make an enum variant have a field which can be another enum variant?

I am trying to see if I can create an enum, whereby I have an enum, with 3 variants, where the last one is a struct that has a field which can be one of the other two enum variants. I.e.

enum A {
    A1,
    A2,
    A3 {
        field: A // This can be either A1 or A2
    }
}

When doing this I get the error saying that A has infinite size, however I was wondering if there was a way to say that although the field must be a variant, it can only be of type A1 or A2, which wouldnt result in an infinite size?

Thanks
Karl

There is not currently any way to subset an enum. What you should do instead is split it up into the parts you want to work with:

enum OneTwo {
    A1,
    A2,
}

enum A {
    A12(OneTwo),
    A3 {
        field: OneTwo
    },
}

This A has the same set of possible values that you wanted.

(Of course, give them better names that make sense in context.)

5 Likes

@kpreid thank you very much for your swift reply!!