Part of Rust for Beginners

Rust Control Flow Tutorial | if else else-if for Beginners (2026)

Sandy LaneSandy Lane

Video: Rust Control Flow Tutorial | if else else-if for Beginners (2026) by Taught by Celeste AI - AI Coding Coach

Take the quiz on the full lesson page
Test what you've read · interactive walkthrough

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 if statements do not require parentheses around conditions.
  • Only one block executes in an if-else if-else chain, stopping at the first true condition.
  • Conditions are evaluated sequentially from top to bottom.
  • The else block acts as a catch-all for any cases not handled by previous conditions.
Ready? Take the quiz on the full lesson page →
Test what you've learned. Watch the lesson and try the interactive quiz on the same page.