beginnerFlow Control
If Else
Conditional branching with if, else if, and else.
If Else
Conditional branching with if, else if, and else.
Difficulty
Beginner
Code
rust
fn main() {
let number = 7;
if number < 0 {
println!("negative");
} else if number == 0 {
println!("zero");
} else {
println!("positive");
}
let x = if true { 1 } else { 0 };
println!("x = {}", x);
}Explanation
if is an expression in Rust. No parentheses needed around conditions.
Key Concepts
- Read the code carefully and understand the data flow
- Try modifying the example to see how it changes behavior
- Run this code in the Rust Playground
Related Topics
Browse more examples in the flow-control category to build a complete understanding of this topic.