Rust while Loops

Rust Tutorial

Rust while Loop – Complete Beginner Guide

Loops are fundamental in programming, and Rust provides several ways to repeat code. While the for loop is commonly preferred, the while loop is powerful when you need condition-based repetition.

In this fully beginner guide, you’ll learn:

  • What a while loop is in Rust

  • Basic syntax and examples

  • Infinite loops with loop

  • Loop control (break, continue)

  • Ownership considerations

  • Common mistakes

  • Best practices

Let’s dive in


What Is a while Loop in Rust?

A while loop repeatedly executes code as long as a condition is true.

Basic syntax:


 

Unlike Rust’s for loop (which works with iterators), the while loop depends on a Boolean condition.


Basic Example – Counting with While


 

Output:

1
2
3
4
5

Important:

  • Condition is checked before each iteration

  • Loop stops when condition becomes false


How while Loop Works Internally

Rust while Loops

Execution flow:

  1. Check condition

  2. If true → execute block

  3. Return to condition

  4. Repeat


Infinite While Loop

If the condition never becomes false:


 

  •  This runs forever unless stopped.

Rust provides a better infinite loop syntax:


 

The loop keyword is more idiomatic for infinite loops.


Breaking Out of a Loop

Use break to exit early.


 

Loop stops at 5.


Skipping Iterations with continue


 

continue skips current iteration.


While vs Loop in Rust

Featurewhileloop
Condition-basedYesNo
Infinite by defaultNoYes
Returns valueNoYes
Common usageConditional repetitionInfinite or complex loops

Returning Values from loop (Not While)

The loop keyword can return values:


 

Output:

10

while cannot return values directly.


While Loop with Ownership

Ownership rules apply inside loops.

Example:


 

This works because ownership is not moved.

But:


 

Here:

  • Ownership moves

  • s becomes invalid

Loops do not bypass ownership rules.


While Loop and Mutable Variables

Since condition must change, variables are often mut.


 

If you forget mut, Rust gives compile-time error.


Using While with Collections

You can manually loop through array using index:


 

But this is less safe than using for.

Rust recommends for for iterating collections.


When Should You Use While?

Use while when:

  • You don’t know number of iterations

  • Loop depends on dynamic condition

  • You need manual control

Examples:

  • User input validation

  • Game loops

  • Waiting for event


Example – Guessing Game Pattern


 

Loop runs until condition satisfied.


Nested While Loops


 

Used in:

  • Matrix operations

  • Pattern printing


Common Beginner Mistakes

  •  Forgetting to update condition variable
  •  Creating infinite loops unintentionally
  •  Forgetting mut keyword
  •  Using while instead of for unnecessarily
  •  Moving ownership accidentally inside loop

While vs For Loop in Rust

Rust prefers for when:

  • Iterating over ranges

  • Iterating collections

  • Working with iterators

Use while when:

  • Condition-based repetition

  • Manual control needed


Safety in Rust While Loops

Rust ensures:

  • No data races

  • No invalid memory access

  • Ownership rules enforced

Even inside loops, Rust maintains memory safety.


Best Practices

  •  Use for for iteration
  •  Use while for condition-based loops
  •  Always ensure condition changes
  •  Keep loops readable
  •  Avoid complex nested loops

Real-World Example – Countdown Timer


 

Clean and predictable.


Frequently Asked Questions (FAQs)

1. What is a while loop in Rust?

A while loop repeats code as long as a Boolean condition remains true.


2. What is the difference between while and loop?

while depends on a condition.
loop runs infinitely unless stopped.


3. Can a while loop return a value?

No. Only loop can return a value using break.


4. When should I use while instead of for?

Use while when repetition depends on a dynamic condition.


5. Can while loops cause infinite loops?

Yes, if the condition never becomes false.


Conclusion

Rust’s while loop is:

  • Simple

  • Condition-based

  • Safe

  • Ownership-aware

You learned:

  • Basic syntax

  • Infinite loops

  • Break and continue

  • Ownership inside loops

  • When to use while

Mastering loops is essential for writing real-world Rust programs.

You may also like...