Skip to content

Latest commit

 

History

History
28 lines (22 loc) · 909 Bytes

how-to-compare-enum-in-rust.md

File metadata and controls

28 lines (22 loc) · 909 Bytes

How to compare enum in Rust

// plain

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

group: rust-enums