Control Flow
A program rarely runs strictly top to bottom: it chooses between options and repeats the same steps. For that Rust has the if condition and three loops: loop repeats its body until something stops it, while repeats while a condition is true, and for walks the elements of a range, an array or a vector. There are no parentheses around a condition, and the braces around a body are always required, even for a single line.
Under the familiar syntax hide the differences that junior interviews probe. A condition must have the type bool: you cannot put a number or a string where a condition goes, as you can in C or Python. if is an expression: it evaluates to a value, so Rust needs no ternary operator ?:, and every branch must produce the same type. loop can also return a value through break, while while and for cannot. There is no C-style for with three parts: instead for walks a range 0..n whose right bound is excluded. Finally, labels such as 'outer let you leave several nested loops at once. This topic walks through all of it layer by layer — from the condition to the labels.
Topic map
- The if expression — the condition is strictly
bool, with no truthiness of numbers or strings;if/else if/elsereturns a value, and every branch must have the same type; in edition 2024 a condition chains with a pattern:if let Some(x) = a && x > 0. - The loop keyword — repeats its body until
break;breakwith a value turns the wholeloopinto an expression;continuemoves to the next step;loopis preferred overwhile true. - The while loop — repeats while the condition is true and may not run even once;
while letkeeps going while a pattern matches;breakwith a value is forbidden here. - for and ranges —
forwalks a sequence without a counter;a..bexcludesb,a..=bincludes it;.rev()reverses,.step_by()sets the step,.iter().enumerate()yields the index and the element. - Loop labels —
'outer:before a loop;break 'outerandcontinue 'outeract on the outer loop;break 'labelwith a value leaves a labelled block.
Common mistakes and traps
| Mistake | Consequence |
|---|---|
Writing if n { … } with a number, as in C or Python | Does not compile: error[E0308]: mismatched types — you need an explicit comparison n != 0 |
Returning values of different types from the if/else branches | error[E0308] — an expression can have only one type |
Using an if without else as a value | error[E0317] — without else the if expression has the type () |
Expecting 1..5 to include the five | The right bound is excluded: you get 1, 2, 3, 4; to include it you need 1..=5 |
Counting down with for i in 5..0 | The range is empty — not a single iteration and no warning; you need (0..5).rev() |
Returning a value through break from while or for | error[E0571] — only loop and a labelled block give back a value |
Putting break in a nested loop to leave both | Only the inner loop stops; for the outer one you need the label break 'outer |
Why it matters in interviews
Conditions and loops are the junior-level filter for "understands that almost everything in Rust is an expression" versus "carries habits over from C, Python or JavaScript." The syntax is trivial, but questions about bool in a condition, if instead of the ternary operator and break with a value from loop come up almost every time.
What interviewers usually check:
- Why
if n { … }with a number does not compile and how Rust differs from languages with truthiness. - Why Rust has no ternary operator and what happens when the
ifbranches return values of different types. - Which loops Rust has, how
loopreturns a value throughbreak, and whywhileandforcannot. - How
a..bdiffers froma..=band how to walk a range in reverse or with a step. - How to leave the outer loop by a label and what a labelled block with a value is.