Q:

Rust program to demonstrate the Match Statement with Enum (Enum Example)

belongs to collection: Rust Enums Programs

0

In this program, we will create an enum Color. Then we will use enum constants with the Match statement.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to demonstrate the Match Statement with Enum is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// Match Statement with Enum

#[derive(Debug)]

enum Color {
   RED,GREEN,YELLOW
}

fn Fun(color:Color) {
   match color {
      Color::RED => {
         println!("Color is RED");
      },
      Color::GREEN => {
         println!("Color is GREEN");
      },
      Color::YELLOW =>{
         println!("Color is YELLOW");
      }
   }
}

fn main() {
   Fun(Color::RED);
   Fun(Color::GREEN);
   Fun(Color::YELLOW);
}

Output:

Color is RED
Color is GREEN
Color is YELLOW

Explanation:

In the above program, we created an enum Color and functions Fun()main(). The enum Color contains constants RED, GREEN, and YELLOW.

The Fun() function accepts the enum as a parameter and here we used enum with match statement. In the main() function, we called the Fun() function and printed the appropriate message.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust program to create an enum with the data type... >>
<< Rust program to get the integer value of enum cons...