ADVERTISEMENT
ADVERTISEMENT

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

Rust | Enum Example: Write a program to demonstrate the Match Statement with Enum.
Submitted by Nidhi, on October 28, 2021

Problem Solution:

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

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.

Rust Enums Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.