In this tutorial you will learn about the Rust If in Let Statement and its application with practical example.
Rust If Let Statement
Rust If Let Statement is a combination of if and let statements in a more concise way, where value of if statement is assigned to let statement.
Syntax:-
1 2 3 4 5 6 |
let var_name= if condition{ //Statement(s) } else{ //Statement(s) } |
Here, if Condition is a Boolean expression that results in either True or False, if it results in True then statements inside if body are executed and result value is assigned to var_name, if it results in False then statements inside else body are executed and result value is assigned to var_name.
Example :-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fn main() { println!("W3Adda Rust If in let Statement"); let num = if true { 10 } else { 20 }; println!("value of num is: {}", num); } |
Output :-