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
- How to use non-capturing groups in Rust regex?
- How to match whitespace with a regex in Rust?
- How to split a string with Rust regex?
- How to build a Rust HashMap from an iterator?
- How do I copy a variable in Rust?
- How to cast pointer to usize in Rust
- How to use Unicode in a regex in Rust?
- How to use regex to match a double quote in Rust?
- How to get a capture group using Rust regex?
- How to match a URL with a regex in Rust?
See more codes...