Q:

Rust program to demonstrate the match statement with multiple values

belongs to collection: Rust match Programs

0

Here, we will demonstrate the match statement with multiple values and print the appropriate message.

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 multiple values is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// match statement with multiple values

fn main() {
    let mut num:i32 = 5;
    
    match num {
    1=> println!("One"),
    2|3=> println!("Two or Three"),
    4|5|6=> println!("Four or Five or Six"),
    7=> println!("Seven"),
    _=> println!("Invalid number")
    };
}

Output:

Monday

Explanation:

Here, we created an integer variable num with an initial value of 5. Then we created a match block with multiple values 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... >>
<< Rust program to demonstrate the match statement wi...