Control Flow
Rust is a systems programming language focused on speed, memory safety, and fearless concurrency — and control flow is how your program makes decisions and repeats work.
Learn Control Flow in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll branch with if / else (which are expressions), and repeat with loop , while , and for .
What You'll Learn in This Lesson
1️⃣ Branching with if / else
An if runs a block when its condition is true , and the condition must be a bool — Rust won't treat numbers as truthy. Because if is an expression, you can assign its result directly to a variable, which replaces the ternary operator from other languages. Both branches must produce the same type.
See how let label = if ... else ... stored a value? That's the expression form. Each arm here yields a &str , so the types line up.
2️⃣ Looping with loop, while, and for
Rust has three loops. loop repeats forever until you break — and uniquely, break can hand a value back out. while repeats while a condition holds. for walks over a range or collection and is the one you'll reach for most.
The range 1..=3 is inclusive of 3. Use 1..3 when you want to stop before the last number. The for loop is preferred because it can't run off the end of a collection.
Your turn. Fill in the blanks marked ___ , then run it.
The classic interview warm-up. Combine a for loop with if / else if / else . Run it with cargo run and compare with the full output.
📋 Quick Reference — Control Flow
Practice quiz
What type must an if condition evaluate to in Rust?
- Any non-zero number
- bool
- i32
- Option
Answer: bool. Rust requires a bool; it never treats numbers as truthy, so write a real comparison like n != 0.
Because if is an expression, what can you do with it?
- Assign its result to a variable
- Skip the else branch when used as a value
- Mix different types in its arms
- Use it only inside loops
Answer: Assign its result to a variable. if is an expression, so let x = if c { a } else { b }; works. Both arms must share a type and the else is required.
Which loop can return a value with break?
- while
- for
- loop
- None of them
Answer: loop. Only loop can hand a value back out, e.g. break counter * 10; the value becomes the loop expression's result.
What does the range 1..=3 produce?
- 1, 2
- 1, 2, 3
- 0, 1, 2, 3
- 2, 3
Answer: 1, 2, 3. 1..=3 is inclusive of 3, yielding 1, 2, 3. The = includes the final number.
What does the range 1..3 produce?
- 1, 2, 3
- 1, 2
- 2, 3
- 0, 1, 2
Answer: 1, 2. 1..3 is exclusive of the end, giving 1 and 2 only. Use 1..=3 to include 3.
Given let r = loop { let mut c = 0; c += 1; if c == 5 { break c * 10; } }; with c counting up from 0, what value does break c * 10 return when c reaches 5?
- 5
- 10
- 50
- 15
Answer: 50. When c equals 5, break c * 10 returns 5 * 10 = 50.
Which loop is generally preferred for iterating a range or collection?
- loop
- while
- for
- recursion
Answer: for. for is preferred because it can't run off the end of a collection, avoiding off-by-one and out-of-bounds bugs.
How do you break out of an outer loop from inside a nested inner loop?
- break all;
- Label the outer loop and write break 'outer;
- return;
- continue outer;
Answer: Label the outer loop and write break 'outer;. Label the loop, e.g. 'outer: loop { ... }, then break 'outer; exits that specific loop.
Why does Rust not need a ternary operator like a ? b : c?
- It bans conditional values
- if is already an expression that returns a value
- match replaces it entirely
- It uses the && operator instead
Answer: if is already an expression that returns a value. Since if is an expression, let x = if cond { a } else { b }; does exactly what a ternary would.
What error appears if you write if number (an integer) as a condition?
- expected bool, found integer
- unused variable
- overflow
- non-exhaustive patterns
Answer: expected bool, found integer. Rust won't treat a number as a condition; it expects a bool, so use a comparison like if number != 0.