C and Rust, side by side
i-- > 0 reverse idiom and a sentinel value that means not found.#include <stdio.h>
int main(void) {
int xs[] = {4, 8, 15, 16, 23, 42};
size_t n = sizeof xs / sizeof xs[0];
int sum = 0;
for (size_t i = 0; i < n; i++) sum += xs[i]; /* an index you manage */
int total = 0;
for (int i = 1; i <= 10; i++) total += i; /* inclusive bound */
for (size_t i = n; i-- > 0;) printf("%d ", xs[i]); /* reverse without underflow */
printf("\nsum=%d total=%d\n", sum, total);
size_t first_big = n; /* sentinel: not found */
for (size_t i = 0; i < n; i++)
if (xs[i] > 20) { first_big = i; break; }
printf("first_big=%zu\n", first_big);
return 0;
}
.rev() instead of the underflow idiom, and a loop whose break yields Some(4) or None.fn main() {
let xs = [4, 8, 15, 16, 23, 42];
let mut sum = 0;
for x in xs { // values, not indices
sum += x;
}
let mut total = 0;
for i in 1..=10 { // `..=` includes 10; `1..10` stops at 9
total += i;
}
for x in xs.iter().rev() { // no unsigned-underflow idiom needed
print!("{x} ");
}
println!("\nsum={sum} total={total}");
let mut i = 0;
let first_big = loop { // `break value` replaces the sentinel
if i == xs.len() { break None; }
if xs[i] > 20 { break Some(i); }
i += 1;
};
println!("first_big={first_big:?}");
}
| C | Rust |
|---|---|
for (i = 0; i < n; i++) with an index you manage. | for x in xs or for i in 0..n; the loop variable is a new binding each time. |
i <= hi never ends when hi is the type's maximum. | lo..=hi includes hi and terminates, even at u8::MAX. |
A sentinel such as n means not found. | loop { ... break Some(i) ... } returns the result directly. |
Understand it
Rust has no for (init; cond; step). for takes anything iterable: a range such as 0..n, an array, a Vec or an iterator chain. The loop variable is a fresh binding in each iteration, and the range is built once before the loop starts. for x in xs visits values directly, so there is no index to get wrong; when you need the index as well, for (i, x) in xs.iter().enumerate() gives both.
a..b is half-open, like i < b; a..=b is closed, like i <= b. The inclusive form matters at type limits. In C, for (unsigned char i = lo; i <= hi; i++) never ends when hi is 255, because i wraps to 0. A Rust port written as lo..hi + 1 panics in a debug build when hi + 1 overflows. lo..=hi visits 255 and stops, because the range records that it has produced its last element instead of computing hi + 1.
Reverse iteration is .rev(), which replaces the for (size_t i = n; i-- > 0;) idiom that avoids an unsigned underflow. while cond { } works as in C. loop { } is an unconditional loop, and it is an expression: break value ends it and becomes its value, which replaces the sentinel pattern where first_big = n means not found.
Labels such as 'outer: for ... with break 'outer replace a goto out of nested loops. Most index searches also have iterator methods (position, find, any), but a clear for or loop is equally idiomatic when it reads better.
The C habit, written in Rust
This is how the C version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.
fn main() {
let xs = [4, 8, 15];
let mut i = xs.len() - 1;
while i >= 0 {
print!("{} ", xs[i]);
i -= 1;
}
}
Try it
Port the commented C functions: count_multiples counts the multiples of k in lo..=hi (zero when lo > hi, and k is at least 1), and first_power_at_least returns the smallest power of two that is at least n (1 for 0 and 1; n is at most 2^31).
What success looks like: count_multiples(250, 255, 5) is 2 without overflow, count_multiples(7, 3, 2) is 0, and first_power_at_least(5) is 8.
Check your understanding
What is total after let mut n = 1; let found = loop { if n * n > 20 { break n; } n += 1; }; let total: u32 = (found..=7).sum();?
Reveal the answer
- 18
- 11
- 13
- It does not compile:
loophas no value
loop ends with break n when n is 5, so found is 5, and 5..=7 includes 7: 5 + 6 + 7 = 18.
Go deeper
Common misconception: “a..=b is just a..b + 1 written differently.”
a..b + 1 computes b + 1, which overflows when b is the type's maximum: a panic in debug builds and an empty range in release builds. a..=b never computes b + 1.
- Classify each loop: Over a collection:
for x in xs. Over numbers: a range. Until a condition with a result:loopwithbreak value. - Check the end bound: Translate
<to..and<=to..=; never writehi + 1on a type that can be at its maximum. - Remove the sentinels: Replace not-found markers such as
nor-1with abreakthat yields anOption.
Transfer challenge: Rewrite the practice program with for x in xs.iter().rev(), then with a labelled loop over two arrays that stops at the first pair whose sum is above 20.
Connections
- Functions Are Expressions: Tail values replace return and the ternary
- From malloc/free to Ownership: One owner frees, exactly once

Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: From for(;;) to Ranges (courseId "rust-for-c-developers", lessonId "c-for-loops-to-ranges") Link: https://crabrix.com/learn/rust-for-c-developers/c-for-loops-to-ranges/ Goal: for iterates over a range or collection; a..b excludes b, a..=b includes it, and loop with break value replaces sentinel variables. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-c-developers/c-for-loops-to-ranges) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.