Hey I'm learning rust and trying to figure out why I can't directly compare two instances of a very simple Enum, I've tried using matches!
(doesn't work) and #[derive(Eq)]
(just forwards the problem to the impl
Here's a snippet demonstrating my issue, and it's output.
#[derive(Debug)]
pub enum MyEnum {
Enum1,
Enum2,
Enum3
}
#[derive(Debug)]
pub enum ThingEnum {
NoOp,
Enum(MyEnum)
}
pub fn test_enum_equate() {
let mut enum1 = Vec::new();
enum1.push(MyEnum::Enum1);
enum1.push(MyEnum::Enum2);
enum1.push(MyEnum::Enum3);
let mut expr = vec![
ThingEnum::NoOp,
ThingEnum::Enum(MyEnum::Enum1),
ThingEnum::Enum(MyEnum::Enum2),
ThingEnum::NoOp,
ThingEnum::Enum(MyEnum::Enum3)
];
for myenum in enum1.iter() {
for entry in expr.iter() {
match entry {
ThingEnum::NoOp => continue,
ThingEnum::Enum(en) => {
// Check if they are the same
println!("matches!({:?}, {:?}) = {}",
myenum, en, matches!(myenum, en)
);
// Need to implement partial eq, WHY?
// println!("{:?} == {:?} -> {}",
// myenum, en, myenum == en
// );
}
}
}
}
}
The output is:
matches!(Enum1, Enum1) = true
matches!(Enum1, Enum2) = true
matches!(Enum1, Enum3) = true
matches!(Enum2, Enum1) = true
matches!(Enum2, Enum2) = true
matches!(Enum2, Enum3) = true
matches!(Enum3, Enum1) = true
matches!(Enum3, Enum2) = true
matches!(Enum3, Enum3) = true
Which isn't the desired effect.
If I just used numbers or strings, this would work fine, but I feel like the usefulness of an Enum should be that there is no associated value, so why is this so difficult to make work?