Rust Control Flow Tutorial | if else else-if for Beginners (2026)
Video: Rust Control Flow Tutorial | if else else-if for Beginners (2026) by Taught by Celeste AI - AI Coding Coach
Watch full page →Rust Control Flow Tutorial: if, else, and else-if Statements
Mastering control flow is essential for writing effective Rust programs. This tutorial covers how to use if, else, and else if statements to make decisions based on conditions, with clear examples demonstrating each construct.
Code
fn main() {
let number = 0;
// Basic if statement: runs block only if condition is true
if number > 0 {
println!("Positive!");
}
// else-if chain: checks multiple conditions in order
else if number < 0 {
println!("Negative!");
}
// else block: runs if none of the above conditions are true
else {
println!("Zero!");
}
}
Key Points
- Rust's
ifstatements do not require parentheses around conditions. - Only one block executes in an
if-else if-elsechain, stopping at the first true condition. - Conditions are evaluated sequentially from top to bottom.
- The
elseblock acts as a catch-all for any cases not handled by previous conditions.