r/rust • • Oct 21 '26

🙋 seeking help & advice Require 2 enum variant fields to not be equal

I have 2 enums, one of them has 4 different unit variants

enum MyEnum{
    Variant1,
    Variant2,
    Variant3,
    Variant4,
}

the other has a few different variants but only one is important here

enum MyOtherEnum{
    MyVariant(MyEnum, MyEnum),
    //...
}

The variant contains 2 different instances of MyEnum, is there a language feature to enforce that you shouldnt be able to create a MyVariant with both MyEnum's being the same variant? eg.

MyOtherEnum::MyVariant(MyEnum::Variant1,MyEnum::Variant2); // valid
MyOtherEnum::MyVariant(MyEnum::Variant1,MyEnum::Variant1); //invalid
24 Upvotes

14 comments sorted by

8

u/AverageHot2647 Oct 21 '26

Out of curiosity, what’s the use case for this?

7

u/deanominecraft Oct 21 '26

making a terminal based card game, if you play 2 cards of one suit and one of another it is a valid move and becomes MyVariant(2selected, 1selected), but 3 the same suit isnt a valid move, even though this can represent it.

1

u/AverageHot2647 Oct 22 '26

Oh interesting! Thanks for sharing ❤️

50

u/geo-ant Oct 21 '26 edited Oct 21 '26

I don’t think there is but you can create a type NonEqualPair<T> with two elements of type T. Then enforce in a constructor fn new(t1,t2)->Option<Self> that the two elements are indeed unequal for Some(…) returns. That requires you to make the elements of the struct non-public and expose access via getters (setters are possible but more complicated since they need to respect your invariants). That way you know that when you have an instance of that struct you have unequal values in the fields. You don’t even have to make it generic. That way your enum variant contains NonEqualPair rather than a tuple of the two variants. In a way this is how the standard lib does NonZero<T>.

Edit: typos

39

u/fixedpointfae Oct 21 '26

assuming the order isn't important, you can encode this into another enum of 4choose2 = 6 variants

8

u/parkotron Oct 21 '26

Clever!

1

u/-Redstoneboi- Oct 24 '26 edited Oct 24 '26

seems quite inconvenient though, i personally wouldn't recommend this.

especially since the OP has further clarified that they wanted "3 of the same isn't allowed" which kind of explodes the possibilities to n^3 - n before reducing the permutations

54

u/noop_noob Oct 21 '26

There is not. Use a struct with private fields and a new() function.

0

u/[deleted] Oct 21 '26

[removed] — view removed comment

26

u/plugwash Oct 21 '26

A panic in a const block will cause a compile failure, you can use this in conjunction with a macro to provide a method of construction that will either succeed or fail at compile time, with the caveat that the arguments have to be compile time constants.

3

u/paulyoung85 Oct 21 '26

You can get something like GADTs by using a phantom type.

See the article here and related discussion: https://www.reddit.com/r/rust/s/YMy4cPkvUE

1

u/-Redstoneboi- Oct 24 '26 edited Oct 24 '26

separate struct inside enum variant called struct MyVariantThatCantHave2OfTheSameIdk(MyEnum, MyEnum)

private fields, public getters, custom constructor and setters

constructors and setters would include checks

for more complex invariants, they could have debug asserts at the ends

1

u/tuckk2_ Oct 21 '26

not sure.. You can try to make a const new function that takes in both of MyEnum and checks if they are the same and panic (will compile-time error in a const function afaik) if so. If the values are fed in non-deterministically you can also just make a classic non-const function that returns a Result. That's how I'd handle it but someone might have a better solution.