Logo
Articles Compilers Libraries Books MiniBooklets Assembly C++ Linux Others Videos
Advertisement

Article by Ayman Alheraki on January 11 2026 10:37 AM

#15 Modern C++ and Rust Programming – A Comparative Educational Guide from Concepts to Applications

#15 Modern C++ and Rust Programming – A Comparative Educational Guide from Concepts to Applications

 

Control Flow

Loops: for, while, loop

Introduction to Looping Constructs

Loops 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 Loops

C++

  • C++ offers several forms of for loops:

    • Traditional for loop: Syntax:

      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:

      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:

  • References: https://en.cppreference.com/w/cpp/language/for https://en.cppreference.com/w/cpp/language/range-for

Rust

while Loops

C++

  • The while loop repeats execution as long as a condition remains true:

  • 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:

  • 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

loop Construct (Rust-specific)

Summary Table

FeatureC++Rust
for loopTraditional & range-based (for(auto& x : container))for element in collection using iterators
while loopwhile(condition) and do-whilewhile condition; no do-while
Infinite loopsfor(;;) or while(true)loop keyword with explicit break
Iteration styleIndex-based or range-basedIterator-based (generalized iteration)
Condition typeImplicit conversions allowed in conditionsMust be bool explicitly
Loop expressionsStatements onlyLoops are expressions; loop returns value

Practical Notes

  • 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.

References

  1. C++ for loops — cppreference https://en.cppreference.com/w/cpp/language/for https://en.cppreference.com/w/cpp/language/range-for

  2. C++ while and do-while loops — cppreference https://en.cppreference.com/w/cpp/language/while https://en.cppreference.com/w/cpp/language/do

  3. 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

  4. 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

Advertisements

Responsive Counter
General Counter
1000731
Daily Counter
2351