Rust program to demonstrate the match statement with enum

Rust | match statement with enum example: Given enum, we have to match the enum values using the match statement.
Submitted by Nidhi, on October 04, 2021

Problem Solution:

Here, we will demonstrate the match statement with enum and print appropriate messages.

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

enum Country {
    INDIA,
    USA,
    UK,
    CANADA,
}

fn main() {
    let country=Country::INDIA;
    
    match country {
    Country::INDIA=>  println!("Welcome to INDIA"),
    Country::UK=>     println!("Welcome to UK"),
    Country::USA=>    println!("Welcome to USA"),
    Country::CANADA=> println!("Welcome to CANADA"),
    _=>               println!("Invalid country")
    };
}

Output:

Welcome to INDIA

Explanation:

Here, we created an enum country. Then we matched the enum of countries using the match statement and printed the appropriate message.

Rust match Programs »





Comments and Discussions!










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