I saw some code like this today:
enum Foo {
Bar(u32),
Baz,
}
// ...
fn is_bar(foo: &Foo) -> bool {
match foo {
Foo::Bar(..) => true,
_ => false,
}
}
which is fine, nothing controversial. But I was wondering if there was a way to do something like
fn is_bar(foo: &Foo) -> bool {
foo == Foo::Bar(..)
}
instead. Obviously the above doesn't compile, but is there a shorter way to say "all I care about is whether enum foo
is variant Bar
"? Or could there be? I think it'd be cool to have the equality operator work with a pattern on one side, but I have no idea what it would mean if you tried to do that with the other comparison operators, so doing it with ==
doesn't make much sense. Has this kind of thing been discussed before in the context of rust?