Skip to content

Rust Loops

Overview

Rust provides three types of loops: loop, while, and for.

🔄 loop Loop

rust
fn main() {
    loop {
        println!("again!");
        break;
    }
}

🔄 while Loop

rust
fn main() {
    let mut number = 3;

    while number != 0 {
        println!("{}!", number);
        number -= 1;
    }

    println!("LIFTOFF!!!");
}

🔄 for Loop

rust
fn main() {
    let a = [10, 20, 30, 40, 50];

    for element in a.iter() {
        println!("the value is: {}", element);
    }
}

Continue learning: Next chapter - Rust Iterators

Content is for learning and research only.