Rust program to demonstrate the match with the block of statements

Rust | match with the block of statements example: Given a number (week number), we have to find the weekday.
Submitted by Nidhi, on October 04, 2021

Problem Solution:

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

Program/Source Code:

The source code to demonstrate the match with the block of statements is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// match with block of statements

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

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

Output:

Match found for 2
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 match with block statements and printed the result.

Rust match Programs »





Comments and Discussions!

Load comments ↻





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