Jim Garrison
Jim Garrison

Reputation: 4276

How can I define "overlapping" enums in Rust?

I would like to have the following two enums. However, the below code fails to compile due to the "duplicate definition" of LargestMagnitude and SmallestMagnitude.

enum SymmetricWhich {
    LargestMagnitude,
    SmallestMagnitude,
    LargestAlgebraic,
    SmallestAlgebraic,
    BothEnds,
}

enum NonsymmetricWhich {
    LargestMagnitude,
    SmallestMagnitude,
    LargestRealPart,
    SmallestRealPart,
    LargestImaginaryPart,
    SmallestImaginaryPart,
}

How can I avoid this duplicate definition? Is there any way without needing to rename enum values within one of them? I considered the possibility of moving the duplicated values to a third enum (given below as CommonWhich), hoping I could then "derive" from is as a base class, but it is not clear to me if (or how) Rust supports this.

enum CommonWhich {
    LargestMagnitude,
    SmallestMagnitude,
}

What is the best way to proceed?

Upvotes: 2

Views: 1419

Answers (1)

Chris Morgan
Chris Morgan

Reputation: 90762

There is not at present any such subtyping in enums; all variants are of exactly one concrete type and what you are trying to do is impossible.

You will need to either rename variants so that they remain disjoint, or else place the enums in different modules.

Upvotes: 4

Related Questions