bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Rust/Rust Tutorial
Rust•Rust Tutorial

Rust Loops

Loops

Loops can execute a block of code as long as a specified condition is reached.

Loops are handy because they save time, reduce errors, and they make code more readable. For example, instead of writing the same line 10 times to print some text, you can use a loop to repeat it for you.

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

loop

loop is the simplest of Rust's three loop types.

It will run forever unless you tell it to stop:

loop {
 println!("This will repeat forever!");
}

Warning

This loop never stops! You will need to press Ctrl + C to end the program.

To stop a loop, use the break keyword:

Example

let mut count = 1;
loop {
  println!("Hello World!");
  if count == 3 {
    break;
  }
count += 1;
}

Example explained

  • This prints "Hello World!" 3 times.
  • It uses a counter to keep track of how many times it has looped.
  • The counter starts at 1 ( let mut count = 1; ).
  • Each time the loop runs, the counter goes up by 1: ( count += 1; ).
  • When it reaches 3, the loop stops.

Return a Value

You can also return a value from a loop using break with a value.

This lets you save the result of the loop into a variable:

Example

let mut count = 1;
let result = loop {
  println!("Hello!");
  if count == 3 {
    break count; // Stop the loop
    and return the number 3
  }
count += 1;
};
println!("The loop stopped at: {}", result);

This loop prints "Hello!" until count reaches 3, then stops and returns that number.

Note

When you save the result of a loop into a variable, you must put a semicolon ( ; ) at the end.

Previous

Rust Match

Next

Rust While Loops

This chapter

Overview
20

Lessons

46m

Read time

1. Rust Tutorial2. Rust Introduction3. Rust Get Started4. Rust Syntax5. Rust Output (Print Text)6. Rust Comments7. Rust Variables8. Rust Data Types9. Rust Constants10. Rust Operators11. Rust Booleans12. Rust If .. Else Conditions13. Rust Match14. Rust Loops15. Rust While Loops16. Rust For Loop17. Rust Functions18. Rust Scope19. Rust Strings20. Rust Ownership

On this page

LoopsloopReturn a Value