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 »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.