rustHow to compare enum in Rust
Enums in Rust can be compared using the == operator. For example, if you have an enum Fruit with variants Apple, Orange, and Banana, you can compare two enum values like this:
let fruit1 = Fruit::Apple;
let fruit2 = Fruit::Orange;
if fruit1 == fruit2 {
println!("The fruits are the same!");
} else {
println!("The fruits are different!");
}
The output of this code would be:
The fruits are different!
The == operator compares the variants of the enum, so in this case it would compare Apple and Orange and return false.
Helpful links
Related
More of Rust
- Regex example to match multiline string in Rust?
- How to use binary regex in Rust?
- How to print a Rust HashMap?
- How to use regex captures in Rust?
- How to join two Rust HashMaps?
- How to use regex with bytes in Rust?
- How to use a custom hash function with a Rust HashMap?
- How to iterate over a Rust HashMap?
- How to check if a Rust HashMap contains a key?
- How to initialize a Rust HashMap?
See more codes...