Skip to main content
In this lesson, we will explore loops and labels in Rust. Looping allows you to execute a block of code repeatedly, and Rust provides several looping constructs including loop, while, and for. Notably, the loop construct repeats indefinitely until it is explicitly stopped with a break statement.
The image is an introduction to looping in Rust, highlighting that looping allows code to execute repeatedly, and Rust offers constructs like loop, while, and for. It also notes that the loop construct repeats indefinitely until stopped with a break statement.

Basic Looping with loop

A basic loop in Rust starts with the loop keyword followed by a block enclosed in curly braces. Inside these braces, you can place the code that you want to repeat indefinitely. For example:
To exit the loop, use the break statement. In the following example, a counter is incremented until it reaches 5. Once the counter equals 5, the break statement terminates the loop:
You can also return a value from a loop by providing a value with the break statement. In this example, the loop increments the counter until it reaches 10, then returns count * 2 which is stored in the result variable:
The output of this program will be:

Loop Labels for Nested Loops

Rust allows you to label loops to make it easier to control nested loops. Labels are defined by a single quote (') followed by the label name. This feature is especially useful when you need to break out of or continue a specific loop in a nested structure.
The image is an introduction to labels in Rust, explaining that labeling loops is useful for controlling nested loops.
Consider the following example where an outer loop is labeled 'outer. An inner loop runs without a label. The inner loop breaks when inner_count reaches 2, but if outer_count reaches 3, the break 'outer statement exits the outer loop entirely:
The expected output for the nested loop example is:

Skipping Iterations with continue

The continue statement in Rust functions similarly to those in languages like C or Python. When encountered, it skips the rest of the current iteration and immediately moves to the next one. In the example below, a for loop iterates over a range from 1 to 5, and skips the iteration when the counter is 3:
This program will output:
  • Basic loops are created using the loop construct and continue indefinitely until a break statement is encountered.
  • Loop labels allow you to manage control flow in nested loop scenarios.
  • Use break to exit a loop immediately and optionally return a value.
  • The continue statement skips the remainder of the loop’s current iteration.
The image is a summary of programming loop concepts, including basic loops, loop labels, and break and continue statements, with a colorful design.
Mastering these control-flow constructs in Rust will help you write more concise, efficient, and readable code when handling various loop-based tasks. For further reading, consider exploring the Rust Programming Language Book.

Watch Video