Conditional Branching (if)
In Rust,
if expressions allow you to branch your code based on conditions. Unlike some other languages, the condition must evaluate to a bool.rust
if is an Expression!
Wait! Because
if is an expression, you can use it on the right side of a let statement to assign a value to a variable based on a condition.rust
Repetition with Loops
Rust has three kinds of loops:
loop, while, and for.1. loop (Infinite)
The
loop keyword tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop using break.rust
2. while (Conditional)
A
while loop runs as long as a condition is true.rust
3. for (Collection)
The
for loop is the most common loop in Rust. It's used to iterate through a collection (like an array).rust
Summary
| Loop | Syntax | Best Use Case... |
|---|---|---|
| loop | loop { ... } | Infinite tasks or returning values |
| while | while x { ... } | Running until a condition changes |
| for | for x in coll | Iterating through collections (Safest!) |
| Range | (1..10) | Counting (1 to 9) |
| break | break value; | Stop loop and return a value |
| continue | continue; | Skip to the next iteration |
| Labels | 'outer: loop | Breaking out of nested loops |
| Logic | && (AND), ` | |
| Safety | for loops prevent index out-of-bounds errors | |
| Modern | Iterators power most complex looping logic in Rust | |
| Condition | Must be a bool (No "truthy" or "falsy" values!) | |
| Assignment | let x = if c { a } else { b }; |