I have code something like this.
#[derive(PartialEq)]
enum FALUDA {
Juice,
Ice,
}
let a: u32 = 0;
if a == FALUDA::Juice {
//Yay
}
If I try this code I am getting below error.
78 | if a == FALUDA::Juice {
| -------- ^^^^^^^^^^^^^ expected `u32`, found `FALUDA`
| |
| expected because this is `u32`
I dont want to cast.
2
Answers
You need to cast the enum value to
u32
(the type ofa
).Here is a complete example:
For simple cases casting really is all one should need. But if you need to do a lot of conversions from/to enum values, you might consider using a crate, such as strum_macros.
— Can we achieve something with match?
Yes, you can implement
PartialEq<FALUDA>
foru32
:But this may prove unstable if you’d decide to change the order of
FALUDA
‘s variants.