ADVERTISEMENT
ADVERTISEMENT

Rust program to demonstrate the match statement

Rust | match statement 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 week number and print the result.

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.

Rust match Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


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.