Q:

Rust program to demonstrate the match statement with enum

belongs to collection: Rust match Programs

0

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

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

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.

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

total answers (1)

<< Rust program to demonstrate the match statement wi...