Rust program to demonstrate the match statement with range

Rust | match statement with range example: Checking range with the match statements.
Submitted by Nidhi, on October 04, 2021

Problem Solution:

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

Program/Source Code:

The source code to demonstrate the match statement with range is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// match statement with range

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

Output:

Five to Six

Explanation:

Here, we created an integer variable num with an initial value of 5. Then we created a match block with range and printed the appropriate message.

Rust match Programs »





Comments and Discussions!

Load comments ↻





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