#[test]
fn full_house_beats_a_flush() {
let a = PokerHand::Flush(6, vec![8, 7, 6, 5, 3]);
let b = PokerHand::FullHouse(7, vec![5, 4]);
assert!(b > a);
}
Although I couldn't find any documentation for it, seems fields aren't used for derived comparison. I definitely don't want to implement PartialEq listing out each possible enum variant pair, as there are many. What are my options?
If I understand correctly, you want the ordering to be the same as comparing two (u8, Vec<u8>)? I suggest a method on the enum to expose something like a (&u8, &[u8]) and then an implementation that uses that method.
They are, it's just that the enum variant has more priority. If every variant will always have the same value for the first u8, it'd probably be better to make it not a field, but a function, which matches on the enum and gives out the result.
For those variants, yes, other variants will have different and unique values. This is basically a representation of a poker hand, when the two hands are equal, then the vector should be compared.
The type of hand is defined by the enum variant (not by the value inside the variant, but by the variant itself): the "greater" hands will be lower in the list of variants.
The contents of hand are defined by the value of the variant - here you might want to use something other then the Vec, to make the comparison order-agnostic (e.g. so that [5, 4] < [4, 6]) and accomodate for the fixed size of the hand. For now, I'll leave is as-is.
With these things considered, we get the following definition:
You probably don't want to rely on the Debug formatter (even if we doesn't think that this is highly inefficient and throws away the information stored in the enum variant's choice, the Debug formatter is in general not machine-readable).
No, other than I didn't see it. It's seems simpler and less fragile, I'll try it and report back.
BTW, the comparison isn't order-agnostic, so[5, 4] > [4, 6]. In other words, there is no reason to keep comparing once an unequal value is found. My implementation above had a bug using fold, which I fixed locally using try-fold.
That's called lexicographical ordering, and it is exactly what #[derive(Ord)] does with fields and it's also what Vec does with elements, exactly because it's usually what you want.