Ad space (banner)
Lesson 8 / 20

Repeating (while and loop)

This lesson covers the while statement for conditional repetition and the loop statement for infinite loops.

while condition { ... } repeats for as long as the condition is true. loop { ... } repeats unconditionally and is exited explicitly with break.

The sample code shows a while statement that repeats while count is 3 or less.

A common early stumble is choosing between loop and while true. Rust convention says to use loop whenever the infinite loop is deliberate.

In professional work, loop is used for repetition with no clear finishing condition — a server waiting for requests, for example.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Rust directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)