Rust provides if expressions (not just statements), loop, while, and for. if branches must return the same type when used as an expression.
if as expression
let label = if score >= 60 { "pass" } else { "fail" };
for loops
for item in collection iterates—prefer over manual index loops when possible.
Self-check
- Can
ifproduce a value? - Which loop is idiomatic for collections?
loop variants
loop { } runs until break. while and for are sugar over pattern matching in many cases. Infinite loops are explicit—no accidental C-style for(;;) footguns if you name intent.
In interviews, mention that if is an expression—this removes a whole class of “assign inside branch” bugs.