Q:

Rust program to demonstrate the match statement

0

Here, we will create an integer variable, and then we will find the weekday from week number and print the result.

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

// Rust program to demonstrate 
// the match statement

fn main() {
    let mut weekNum:i32 = 2;

    let day = match weekNum {
    1=> "Sunday",
    2=> "Monday",
    3=> "Tuesday",
    4=> "Wednesday",
    5=> "Thursday",
    6=> "Friday",
    7=> "Saturday",
    _=> "Invalid week number"
    };
    
    println!("Week day is: {}",day);
}

Output:

Week day is: Monday

Explanation:

Here, we created an integer variable weekNum with an initial value of 2. Then we found the weekday from week number using the match statement and printed the result.

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now