Learn Before
Concept
if-else - Rust Programming
An if expression in Rust allows you to choose which code to execute based on a certain condition. You specify a condition and then the code that will run only if the condition is true, and the code will not run if the condition is false.
let x = 15; if x < 10 { println!("x is less than 10"); } else if x < 20 { println!("x is between 10 and 20"); } else { println!("x is greater than or equal to 20"); }
In this example, the variable `x` is assigned the value `15`. The first if expression checks if the value of `x` is less than 10, if it's not true, it goes to the next `else if` which checks if x is less than 20, if it's true, it will print "x is between 10 and 20", otherwise the last block of code will execute and print "x is greater than or equal to 20".
0
1
Updated 2023-02-03
Tags
Object-Oriented Programming