Rust logo
Rust 1.26.0
a77568041

Loops

There is a new edition of the book and this is an old link.

Rust has three kinds of loops: loop, while, and for. The loop keyword tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop. while loops evaluate a block of code until a condition ceases to be true. A for loop executes some code for each item in a collection.

loop {
    println!("again!");
}

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

let a = [10, 20, 30, 40, 50];
for element in a.iter() {
    println!("the value is: {}", element);
}Run

Here are the relevant sections in the new and old books: