🔀 Control Flow in Rust
Why Control Flow?
Control flow lets your programs make decisions and repeat actions. Rust gives you powerful, safe, and expressive tools for this!
❓ if & else
Use if to run code based on a condition:
rust
Loading…
- Conditions must be a bool (no
if x = 5mistakes!) - 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 (likedefault) - Patterns can be values, ranges, or even destructured data
🔁 Loops: repeat things!
1. loop (infinite)
rust
Loading…
- Use
breakto exit,continueto skip to the next iteration
2. while
rust
Loading…
3. for
rust
Loading…
foris the most common loop in Rust—safe, concise, and works with ranges or collections
🧠 Summary
- Use
if/elsefor decisions - Use
matchfor powerful pattern matching - Use
loop,while, andforfor repetition - Rust’s control flow is safe and expressive
🦀 Pro Tip: Rust’s compiler checks your patterns—no missing cases, no silent bugs!