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
- How to create enum from number in Rust
- How to create enum from int in Rust
- How to print enum in Rust
- How to use enum as hashmap key in Rust
- Get enum value by index in Rust
- Get certain enum value in Rust
- How to use fmt for enum in Rust
- How to serialize enum in Rust
- How to create enum from string in Rust
More of Rust
- How to replace a capture group using Rust regex?
- How to replace all matches using Rust regex?
- How to convert a Rust HashMap to a BTreeMap?
- How to replace strings using Rust regex?
- How to implement PartialEq for a Rust HashMap?
- Regex example to match multiline string in Rust?
- How to match a URL with a regex in Rust?
- How to split a string with Rust regex?
- How to use regex lookbehind in Rust?
- How to use regex lookahead in Rust?
See more codes...