SimplifyC++ Article
#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
forloops:Traditional
forloop: 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
forloop (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
Rust provides a powerful
forloop 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
forloop abstracts the iterator pattern, requiring the collection to implement theIntoIteratortrait. 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 Loops
C++
The
whileloop 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-whileloop, 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
Rust supports the
whileloop 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 usingbreak.Rust does not have a built-in
do-whileconstruct, but similar behavior can be emulated usingloopwith conditionalbreak(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
loopkeyword creates an infinite loop with explicit exit points viabreakorreturn.Syntax:
loop {// codeif 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
loopexpression 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
Summary Table
| 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 |
Practical Notes
C++ developers should prefer range-based
forloops over traditional index-based loops to avoid off-by-one errors and increase readability.Rust’s iterator-based
forloops provide more flexibility and safety, encouraging functional-style code and composability.The absence of
do-whilein Rust requires creative use ofloopandbreakto simulate post-condition loops.Use Rust’s
loopfor indefinite repetition where exit conditions vary, benefiting from its expressive and safe design.
References
C++
forloops — cppreference https://en.cppreference.com/w/cpp/language/for https://en.cppreference.com/w/cpp/language/range-forC++
whileanddo-whileloops — cppreference https://en.cppreference.com/w/cpp/language/while https://en.cppreference.com/w/cpp/language/doRust
forloops — 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-forRust
whileandloop— 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