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

This example demonstrates how to use loop in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.

Key Concepts

  • Rust's strong type system catches errors at compile time
  • Ownership and borrowing rules ensure memory safety
  • Pattern matching makes code expressive and exhaustive

Related Topics

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

More Flow Control Examples