Why did the Rust loop break up with the condition? It said "I just need some space."
You've already seen if and for in passing. This chapter slows down
and looks at them on purpose, plus the other two loop forms (while
and loop) and the keywords that control them (break and
continue).
if / else / else ifThe usage is unsurprising:
if x > 0 {
println!("positive");
} else if x < 0 {
println!("negative");
} else {
println!("zero");
}
Two things to call out:
The condition is a bool. No truthy strings, no zero-as-false, no
parentheses required around the condition.
The whole if is itself an expression. You can use it on the
right-hand side of a let binding:
let label = if x >= 0 { "non-negative" } else { "negative" };
Both branches have to produce the same type, and there's no trailing semicolon on the value-producing expression in each branch (just like a function body, see chapter 4).
for loopsfor walks anything that produces an iterator. Here's how it works:
for i in 0..5 { // 0, 1, 2, 3, 4
println!("{i}");
}
for word in ["hi", "rust"] {
println!("{word}");
}
0..5 is a range: a value that produces the integers from 0 up
to (but not including) 5. The inclusive form is 0..=5, which
also yields 5. Both work as iterators and as patterns in match
(seen in the password chapter later).
For larger collections, you'll usually iterate over a Vec, a
slice, a HashMap, or the result of s.chars(). Iterators get
their own chapter (chapter 16); for now, "anything you can put on
the right of for x in ..." is enough.
while and loopwhile runs as long as a condition is true:
let mut n = 10;
while n > 0 {
println!("{n}");
n -= 1;
}
loop runs forever, until you break out of it. Useful when the
exit condition isn't a simple boolean check at the top:
let mut attempts = 0;
loop {
attempts += 1;
if try_connect() { break; }
if attempts > 10 { break; }
}
loop can also produce a value: pass an expression to break and
the whole loop evaluates to it.
let answer = loop {
let guess = read_guess();
if guess == 42 { break guess; }
};
break and continueBoth keywords control the innermost loop:
break exits the loop immediately.continue skips the rest of the current iteration and starts the
next one.for n in 0..10 {
if n % 2 == 1 { continue; } // skip odd
if n > 6 { break; } // stop at 8
println!("{n}"); // 0, 2, 4, 6
}
A useful rule of thumb:
for when you know what you're iterating over (a range, a
slice, a map, the chars of a string).while when the exit condition is a simple "keep going while
X is true".loop only when neither of the above fits, usually because
the exit condition is in the middle of the body.Most code reaches for for. Iterators (covered later in the
course) make for even more powerful.
Ferris the crab is a creature of simple needs. Two things determine
his mood on any given day: how hungry he is (on a 0..=10 scale)
and how many naps he's managed to fit in.
Implement ferris_mood(hunger, naps) returning a &'static str,
following these rules:
| Condition | Mood |
|---|---|
hunger >= 8 | "Hangry" |
hunger >= 5 and naps == 0 | "Grumpy" |
naps >= 3 | "Sleepy" |
| anything else | "Content" |
&'static str just means "a borrowed string slice that lives for
the whole program". String literals like "Hangry" are baked into
your compiled binary, so the text is around for as long as the
program is running. The 'static lifetime is just the compiler's
way of saying "this reference will never dangle." If you've written
C, it's the same intuition as a const char * pointing at a string
literal. Lifetimes get a proper introduction in chapter 12 alongside
ownership and borrowing; for now the only thing to take away is
"string literals are always safe to return as &'static str."
Combining conditions. The "Grumpy" rule needs both parts to
be true. Rust spells this && (logical AND). Its sibling || is
logical OR. Both short-circuit: if the left side already decides
the answer, the right side isn't evaluated.
Order matters. An if/else if/else chain is checked
top-to-bottom and stops at the first match. If you put the naps
check before the hunger check, a hungry crab who happens to have
napped a lot will get classified as "Sleepy" instead of
"Hangry". The tests deliberately include cases (like
ferris_mood(9, 5)) that only pass with the right ordering.
if/else if chain decides everything: top to
bottom, first match wins. Translate the rule table line by line
and the order falls out for you."Grumpy" rule needs both conditions to be true. Combine
them with && (logical AND).fn ferris_mood(hunger: u32, naps: u32) -> &'static str {
if hunger >= 8 {
"Hangry"
} else if hunger >= 5 && naps == 0 {
"Grumpy"
} else if naps >= 3 {
"Sleepy"
} else {
"Content"
}
}
n! is 1 * 2 * 3 * ... * n. By convention, 0! == 1. Build it
up with a running accumulator and a for loop over an inclusive
range.
The accumulator pattern shows up everywhere once you start writing
loops: let mut acc = ...; for x in ... { acc = ... }; acc. Note
the mut: bindings are immutable by default, and the loop body
needs to update acc, so you have to opt in.
let mut acc: u32 = 1; outside the loop, for i in 1..=n { ... }
inside. Return acc at the end.acc *= i;. Both mut on the binding
and *= for the compound assignment are needed.fn factorial(n: u32) -> u32 {
let mut acc: u32 = 1;
for i in 1..=n {
acc *= i;
}
acc
}
Now you have a slice of integers and want to know how many of them
are even. The natural approach is a for loop over the slice with a
counter that you bump on every match.
This is a good place to use continue: skip the odds early and the
"do work" branch ends up uncluttered.
let mut count = 0u32; plus a for n in numbers loop. The
suffix 0u32 pins the integer type so you don't need a separate
annotation.for n in numbers over a &[i32] yields &i32. The %
operator works through the reference, so n % 2 Just Works.
continue skips the rest of the current iteration.fn count_evens(numbers: &[i32]) -> u32 {
let mut count = 0u32;
for n in numbers {
if n % 2 != 0 { continue; }
count += 1;
}
count
}
How many digits does a number have? 0 has one digit; everything
else is "divide by 10 and count how many times you can do it before
hitting zero". That's a natural while loop: keep going as long as
the number is non-zero, dividing it down each step.
This is the inverse of a for loop (like the one you wrote for factorial).
With factorial, you knew up front how many times to loop. Here, you don't:
you have to keep dividing until the number runs out. That's exactly what
while is for.
n == 0 returning 1. Otherwise, divide by 10 in
a while loop and count the iterations.let mut n = n; so you can mutate it
without changing the signature. Loop while n > 0, dividing by
10 and bumping a counter.fn digit_count(n: u32) -> u32 {
if n == 0 { return 1; }
let mut n = n;
let mut count = 0u32;
while n > 0 {
n /= 10;
count += 1;
}
count
}
You wrote a three-way classifier with if/else if/else, two
accumulator-style loops (a for over a range and a for over a slice with
continue), and a while loop where the iteration count isn't known up front.
What we learned
if/elseis an expression, not just a statement. It can sit on the right oflet, be returned from a function, or appear anywhere a value is expected. Both branches must have the same type.- Conditions are bare
boolexpressions. No parentheses required, no implicit conversion from integers or strings.for x in iteris the default loop. Ranges (0..n,0..=n), slices, vectors, and most other collections all produce iterators you can put on the right.while condruns as long as the condition is true. Reach for it when the iteration count depends on values computed inside the loop (like "divide until zero").loopruns forever until youbreak. It can also produce a value:let x = loop { ...; break value; };.breakexits the innermost loop;continueskips to the next iteration. Acontinueto early-out the boring case usually reads better than nesting the work inside anif.- The accumulator pattern (
let mut acc = ...; for ... { acc = ...; }) is the how you can "compute one value from many". Once you meet iterators in chapter 15, methods likesum,count, andfoldwill replace many of these by-hand loops.