🔀 Control Flow in Rust


❓ if & else

Use if to run code based on a condition:

rust
Loading…
  • Conditions must be a bool (no if x = 5 mistakes!)
  • You can chain with else if

🎯 match: Pattern Matching

match lets you compare a value against patterns (like a supercharged switch):

rust
Loading…
  • The _ pattern is a catch-all (like default)
  • Patterns can be values, ranges, or even destructured data

🔁 Loops: repeat things!

1. loop (infinite)

rust
Loading…
  • Use break to exit, continue to skip to the next iteration

2. while

rust
Loading…

3. for

rust
Loading…
  • for is the most common loop in Rust—safe, concise, and works with ranges or collections

🧠 Summary

  • Use if/else for decisions
  • Use match for powerful pattern matching
  • Use loop, while, and for for repetition
  • Rust’s control flow is safe and expressive