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.
Program/Source Code:
The source code to demonstrate the Match Statement with Enum is given below. The given program is compiled and executed successfully.
Output:
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