Chapter 15: Rust While Loops

 Rust while loops

The while loop is probably the one that feels most familiar if you’ve programmed in C, Java, Python, JavaScript, etc. It’s very straightforward:

“As long as this condition is true, keep repeating the block.”

Rust’s version is clean, safe, and has almost no surprises — but it also comes with some Rust-specific habits and patterns that make it even more powerful when used correctly.

Let me explain it like your personal teacher sitting next to you: slowly, step by step, with many practical examples you can copy-paste right now, some Hyderabad-flavored comments, and comparisons so you see exactly how it differs from other languages.

1. Basic Syntax of while

Rust
  • Condition must be a bool (no automatic conversion from int like in C!)
  • Checked before every iteration
  • If condition is false at the very beginning → body never runs (zero times is possible)

Simple first example:

Rust

Output:

text

Classic teacher warning: Always make sure something inside the loop makes the condition eventually false — otherwise infinite loop (Rust catches many infinite-loop bugs at compile time in other constructs, but while relies on you).

2. Real-world style: Temperature simulation (Hyderabad summer)

Rust

3. while with user input simulation (common pattern)

Rust

In real code you would use std::io to actually read input — but this shows the loop logic clearly.

4. while let — The Rust-special version (very common!)

while let combines while + let + pattern matching — super useful with Option, Result, iterators, etc.

Classic example — reading lines until None:

Rust

Even more common with Option:

Rust

5. while vs loop — Quick comparison (students always ask)

Feature while loop
When condition checked Before body Never checked automatically
Typical use “repeat while something is true” “repeat forever until I break”
Can return value No (but can with break in loop) Yes — break value;
Most common in Rust Less than for Used when no natural iterator exists
Risk of infinite loop Possible if forget to change var Very explicit — you must write break

In real Rust code written 2025–2026:

  • for → ~60–70% of loops
  • loop → ~20–30%
  • while → ~10–15% (mostly while let)

6. Nested while + labels (rare but good to know)

Rust

Teacher Summary — When to use while

Use while when:

  • You have a clear boolean condition that will become false eventually
  • You don’t have a natural collection/range to iterate over (for is usually better then)
  • You want to keep checking something until a state changes (file reading, game loops, waiting for condition)

But in modern Rust — prefer for with iterators whenever possible — it’s safer and more idiomatic.

Practice challenge for today:

  1. Write a while loop that counts down from 10 to 1 (print “Liftoff!” at 0)
  2. Make a while let version that processes items from a Vec<Option<i32>> until None
  3. Simulate a simple game loop: keep asking for input until user types “quit”

Want me to show any of these solutions? Or move to for loops in more depth, iterators, functions, or ownership inside loops?

Just tell me — class is still in session! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *