Open In App

Loops in Rust

Last Updated : 08 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A loop is a programming structure that repeats a sequence of instructions until a specific condition is satisfied. Similar to other programming languages, Rust also has two types of loops:

  • Indefinite Loop: While loop and Loop
  • Definite Loops: For loop

Let’s explore them in detail.

While Loop

A simple way of explaining a while loop is that the loop runs until the given condition is met. For the below loop we have taken user input, and we decrement until it reaches 1.

While the loop starts by checking the condition, Here, we create the variable with local scope to the loop, or we can use an already existing variable which marks the start of the loop to use in use the loop used to check the condition. If it is true, then the loop is executed. For this reason, it is also called the Entry control loop. Once the condition is true, the statements in the loop body are executed and the loop contains a variable that is updated for each iteration. When the condition becomes false or explicitly asked to break, the loop terminates which marks the end of its life cycle.

Syntax:

while condition {
statements;
}

Example 1: A Rust program to print numbers from 10 to 1.

Rust
fn main() {

    let mut n = 10;

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

Output:

10
9
8
7
6
5
4
3
2
1

Example 2: Program to print multiplication table of 2 using while loop.

Rust
fn main() {
    let mut n = 1;

    while n<=10 {
        println!("{}", n*2);
        n+=1;
    }
}

Output:

2
4
6
8
10
12
14
16
18
20

For loop

The for loop similar to the while loop has a definite start and endpoint as well as the increment for each iteration. In the below loop we have taken the user input and check whether the input is below or above 5.

For loop is somewhat different from while loop. Here also, we create the variable with local scope to the loop, or we can use an already existing variable which marks the start of the loop to use in use the loop. The declared variable is also used to declare the termination condition of the loop. For all the values in between initial value and termination value the return a boolean value. It is also an Entry Control Loop as the condition is checked prior to the execution of the loop statements. Then the condition is checked to true the loop will be executed and if the value is false or explicitly defined to be exited the loop terminates. The iteration value is updated for each iteration.

Syntax:

for iterator in range{
statements;
}

Example 1: Program to print numbers from 1 to 10 using For loop.

Rust
fn main() {

    for  n in 1..11 {
        println!("{}", n);
    }
}

Output:

1
2
3
4
5
6
7
8
9
10

Example 2: Program to print multiplication table of 2 using For loop.

Rust
fn main() {

    for i in 1..11 {
        println!("{}", i*2);
    }
}

Output:

2
4
6
8
10
12
14
16
18
20

Loop statement

Rust also supports another looping statement called the loop statement. Loop statement executes until there is a break statement. Break statement (or) break keyword is used to exit the loop.

Syntax:

loop {
statements;
// for exit
break;
}

Example 1: Program to print multiplication table of 2 using the Loop statement.

Rust
fn main() {
    let mut n = 1;

    loop {
        println!("{}", n*2);
        n+=1;
        if n>10{
            break;
        }
    }
}

Output:

2
4
6
8
10
12
14
16
18
20


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads