It’s often useful to execute a block of code more than once. For this task, Rust provides several loops, which will run through the code inside the loop body to the end and then start immediately back at the beginning. To experiment with loops, let’s make a new project called loops. Rust has three kinds of loops: loop, while, and for. Let’s try each one. # Repeating Code with Loop The loop keyword tells Rust to execute a block of code over and over again forever or until you explicitly tell it to stop. As an example, change the src/main.rs file in your loops directory to look like this: Filename: src/main.rs ```rust fn main() { loop { println!("again!"); } } ``` Fortunately, Rust also provides a way to break out of a loop using code. You can place the `break` keyword within the loop to tell the program when to stop executing the loop. We also used `continue`, which in a loop tells the program to skip over any remaining code in this iteration of the loop and go to the next iteration. # Returning Values from Loops One of the uses of a loop is to retry an operation you know might fail, such as checking whether a thread has completed its job. You might also need to pass the result of that operation out of the loop to the rest of your code. To do this, you can add the value you want returned after the break expression you use to stop the loop; that value will be returned out of the loop so you can use it, as shown here: ```rust fn main() { let mut counter = 0; let result = loop { counter += 1; if counter == 10 { break counter * 2; } }; println!("The result is {result}"); } ``` You can also return from inside a loop. While break only exits the current loop, return always exits the current function. # Loop Labels to Disambiguate Between Multiple Loops If you have loops within loops, break and continue apply to the innermost loop at that point. You can optionally specify a loop label on a loop that you can then use with break or continue to specify that those keywords apply to the labeled loop instead of the innermost loop. Loop labels must begin with a single quote. Here’s an example with two nested loops: ```rust fn main() { let mut count = 0; 'counting_up: loop { println!("count = {count}"); let mut remaining = 10; loop { println!("remaining = {remaining}"); if remaining == 9 { break; } if count == 2 { break 'counting_up; } remaining -= 1; } count += 1; } println!("End count = {count}"); } ``` # Conditional Loops with while A program will often need to evaluate a condition within a loop. While the condition is true, the loop runs. When the condition ceases to be true, the program calls break, stopping the loop. It’s possible to implement behavior like this using a combination of loop, if, else, and break; you could try that now in a program, if you’d like. However, this pattern is so common that Rust has a built-in language construct for it, called a while loop. In Listing 3-3, we use while to loop the program three times, counting down each time, and then, after the loop, print a message and exit. Filename: src/main.rs ```rust fn main() { let mut number = 3; while number != 0 { println!("{number}!"); number -= 1; } println!("LIFTOFF!!!"); } ``` # Looping Through a Collection with for You can also use the while construct to loop over the elements of a collection, such as an array. For example, the loop in Listing 3-4 prints each element in the array a. Filename: src/main.rs ```rust fn main() { let a = [10, 20, 30, 40, 50]; let mut index = 0; while index < 5 { println!("the value is: {}", a[index]); index += 1; } } ``` ## Looping Through a Collection with `for` As a more concise alternative, you can use a `for` loop and execute some code for each item in a collection. A `for` loop looks like the code in Listing 3-5. ```rust fn main() { let a = [10, 20, 30, 40, 50]; for element in a { println!("the value is: {element}"); } } ``` ```bash $ cargo run -q the value is: 10 the value is: 20 the value is: 30 the value is: 40 the value is: 50 ``` **:: Reference ::** [Control Flow - The Rust Programming Language](https://doc.rust-lang.org/book/ch03-05-control-flow.html)