Rust Program to Create an enum class


Create an enum class

Rust Programming Language


Create an enum class


Problem


To create an enum class in Rust, we use the enum keyword followed by the name of the enum class and the possible values enclosed in braces. Here is an example program.

Input


enum Color {
    Red,
    Green,
    Blue,
}

fn main() {
    let color = Color::Red;
    match color {
        Color::Red => println!("The color is red!"),
        Color::Green => println!("The color is green!"),
        Color::Blue => println!("The color is blue!"),
    }
}{codeBox}


Output


The color is red!{codeBox}


Explanation


In this example, we define an enum class called Color that has three possible values: Red, Green, and Blue. In the main function, we create a variable called color and set it to Color::Red. We then use a match statement to print out a message depending on the value of color.

Post a Comment