if / else
Try it yourself →Executes code based on a condition.
Syntax
if condition { } else { }Description
Conditions have NO PARENTHESES, { } blocks are mandatory. if is an expression; it can also yield a value.
Example
fn main() {
let score = 75;
if score >= 50 {
println!("Passed");
} else {
println!("Failed");
}
}Expected output
Passed
When to use?
Flow branching.