ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



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.