Article by Ayman Alheraki on January 11 2026 10:37 AM
for, while, loopLoops enable repeated execution of code blocks based on conditions or over sequences. C++ and Rust both support multiple looping constructs, with differences in syntax, semantics, and idiomatic usage shaped by each language’s goals.
for LoopsC++ offers several forms of for loops:
Traditional for loop:
Syntax:
for (initialization; condition; increment) { // loop body}This form is versatile and supports index-based iteration, commonly used for iterating over arrays or containers (cppreference).
Range-based for loop (introduced in C++11):
Syntax:
for (auto& element : container) { // use element}This loop iterates over elements in a container or range, simplifying iteration and preventing common indexing errors (cppreference).
The range-based loop is preferred for safer, more readable code, especially with STL containers.
Example:
std::vector<int> v = {1, 2, 3};for (auto& elem : v) { std::cout << elem << "\n";}References: https://en.cppreference.com/w/cpp/language/for https://en.cppreference.com/w/cpp/language/range-for
Rust provides a powerful for loop that iterates over iterators, which generalizes over arrays, ranges, collections, and custom iterator types (Rust Reference).
Syntax:
for element in collection { // loop body}The Rust for loop abstracts the iterator pattern, requiring the collection to implement the IntoIterator trait. This design promotes expressive, safe, and flexible looping.
Example:
let v = vec![1, 2, 3];for elem in &v { println!("{}", elem);}References: https://doc.rust-lang.org/reference/expressions/for-in-expr.html https://doc.rust-lang.org/book/ch03-05-control-flow.html#looping-through-a-collection-with-for
while LoopsThe while loop repeats execution as long as a condition remains true:
while (condition) { // loop body}The condition is evaluated before each iteration; if false initially, the loop body does not execute.
C++ also has a do-while loop, which executes the loop body at least once before checking the condition:
do { // loop body} while (condition);Both forms are standard, useful for condition-driven repetition where the number of iterations is not known upfront (cppreference).
References: https://en.cppreference.com/w/cpp/language/while https://en.cppreference.com/w/cpp/language/do
Rust supports the while loop with syntax similar to C++:
while condition { // loop body}The condition must be a boolean expression (no implicit conversion allowed).
Rust also supports loop (infinite loops) which can be exited explicitly using break.
Rust does not have a built-in do-while construct, but similar behavior can be emulated using loop with conditional break (Rust Book).
References: https://doc.rust-lang.org/book/ch03-05-control-flow.html#repetition-with-while https://doc.rust-lang.org/reference/expressions/loop-expr.html
loop Construct (Rust-specific)Rust’s loop keyword creates an infinite loop with explicit exit points via break or return.
Syntax:
loop { // code if some_condition { break; }}This construct is idiomatic for indefinite looping scenarios and is more flexible than while(true) in C++ due to its integration with pattern matching and expressions.
The loop expression can return values, allowing patterns like:
let result = loop { if some_condition { break value; }};References: https://doc.rust-lang.org/book/ch03-05-control-flow.html#infinite-loops-with-loop https://doc.rust-lang.org/reference/expressions/loop-expr.html
| Feature | C++ | Rust |
|---|---|---|
for loop | Traditional & range-based (for(auto& x : container)) | for element in collection using iterators |
while loop | while(condition) and do-while | while condition; no do-while |
| Infinite loops | for(;;) or while(true) | loop keyword with explicit break |
| Iteration style | Index-based or range-based | Iterator-based (generalized iteration) |
| Condition type | Implicit conversions allowed in conditions | Must be bool explicitly |
| Loop expressions | Statements only | Loops are expressions; loop returns value |
C++ developers should prefer range-based for loops over traditional index-based loops to avoid off-by-one errors and increase readability.
Rust’s iterator-based for loops provide more flexibility and safety, encouraging functional-style code and composability.
The absence of do-while in Rust requires creative use of loop and break to simulate post-condition loops.
Use Rust’s loop for indefinite repetition where exit conditions vary, benefiting from its expressive and safe design.
C++ for loops — cppreference
https://en.cppreference.com/w/cpp/language/for
https://en.cppreference.com/w/cpp/language/range-for
C++ while and do-while loops — cppreference
https://en.cppreference.com/w/cpp/language/while
https://en.cppreference.com/w/cpp/language/do
Rust for loops — Rust Reference
https://doc.rust-lang.org/reference/expressions/for-in-expr.html
https://doc.rust-lang.org/book/ch03-05-control-flow.html#looping-through-a-collection-with-for
Rust while and loop — Rust Book and Reference
https://doc.rust-lang.org/book/ch03-05-control-flow.html#repetition-with-while
https://doc.rust-lang.org/reference/expressions/loop-expr.html