RRust By Example
beginnerFlow Control

For Loop with Range

Iterate over ranges with for loop.

For Loop with Range

Iterate over ranges with for loop.

Difficulty

Beginner

Code

rust
fn main() {
    for i in 0..5 {
        println!("{}", i);
    }
    println!("---");
    for i in (0..5).rev() {
        println!("{}", i);
    }
}

Explanation

for iterates over ranges and iterators. Most common loop in Rust.

Key Concepts

  • Read the code carefully and understand the data flow
  • Try modifying the example to see how it changes behavior
  • Run this code in the Rust Playground

Related Topics

Browse more examples in the flow-control category to build a complete understanding of this topic.

More Flow Control Examples