RRust By Example
beginnerFlow Control

While Loop

Loop with a condition check at the top.

While Loop

Loop with a condition check at the top.

Difficulty

Beginner

Code

rust
fn main() {
    let mut n = 1;
    while n < 1000 {
        n *= 2;
    }
    println!("n = {}", n);
}

Explanation

while loops check conditions before each iteration.

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