RRust By Example
beginnerFlow Control

Loop

Infinite loops with break and continue.

Loop

Infinite loops with break and continue.

Difficulty

Beginner

Code

rust
fn main() {
    let mut count = 0;
    let result = loop {
        count += 1;
        if count == 10 {
            break count * 2;
        }
    };
    println!("result: {}", result);
}

Explanation

loop creates infinite loops. break value returns a value.

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