Rust program to demonstrate the if let statement

Rust | if let statement example: Write an example to compare floating-point value using if let statement.
Submitted by Nidhi, on October 07, 2021

Problem Solution:

In this program, we will demonstrate the if let statement by comparing a floating-point value and print the result.

Program/Source Code:

The source code to demonstrate the if let statement is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// example of if let statement

fn main() 
{
    let num: f32 = 16.0;
    
    let result = if let 16.0=num{
        "Hello"
    }
    else{
        "Hiii"
    };
    
    println!("{}",result);
}

Output:

Hello

Explanation:

Here, we created a variable num of f32 type with an initial value of 16.0. Then we compared the value of num using the if let statement and return the string value. After that, we printed the returned value.

Rust if/else Programs »





Comments and Discussions!

Load comments ↻





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