How to use loops In Rust?

Introduction

This article teaches how to use Loop in Rust programming language. Loops are one of the important topics in Rust. Loops are used when we want repetition in values we used loop. In Rust programming language, there are three types of loops.

  • for
  • while
  • loop

Rust's most fundamental loop is the loop, which runs the code block endlessly until a break statement is reached. Contrarily, the while loop runs the code block repeatedly if the condition stated in the loop header is true. Finally, the for loop executes the code block once for each item in the collection as it loops over a list of objects.

For Loop in Rust

In For loop, the Program will execute a specific number of times like an array. The syntax of for loop is given below.

Syntax

for <temporary variable> <expression lower bond to uper bond>
{
  // statements
}

Example

fn main() {
    let mut arr = [0; 5]; // Declare an array of length 5 with initial value 0

    
   // Here For the loop starts to take values in arr
    for i in 0..arr.len()  
    {
    
      arr[i] = i+1;
    }
    // Here For the loop start to print the value through arr
    for i in 0..arr.len() {
        println!("Value at index {} is: {}", i, arr[i]);
    }
}

Output

output

Explanation

In this example, firstly, we declare an array of five sizes, then we declare for loop for inserting values in an array. After inserting values in an array, we print those values with the help of for loop. We can see the result in the above output.

While Loop in Rust

In the While loop, first, check the condition, then execute and last, increase the value.

Syntax

while <condtion> {
   // statements
   <increment>
} // end while loop

 Example

fn main(){

    let mut a = 5;
    while a>10                  // here we start while loop
    {
      println!("{}",a);         // print the value of a until condition satisfy 
      a=a+1;
     }
}

Explanation

In this example, we declare a variable "a" and that is also a mutable type; after that, we declare a while loop and write with conditions. When the condition fails, the loop ends.

Output

output loaded

Loop in Rust

A Loop is nothing but a While loop, it works as a While loop, but in this case, we must add an endpoint of the loop, like break statements. Examples are given below.

Example

fn main() {
    let mut a = 0;

    let result = loop {
        a += 1;

        if a == 10 {
            break a ;
        }
    };

    println!("The Output is {result}");
}

Explanation

In this example, we use Loop, so adding an endpoint with break statements is compulsory when we use a loop. So in this example, we declare a variable that is mutable type. After that, we declare a loop and add an endpoint in "if" statements with a break. You can see the output there given below.

Output

When to use these loops?

We just saw that Rust offers three types of loops:

The loop loop- This loop runs indefinitely until a break statement is encountered. It is useful when you must repeatedly execute a code block until a certain condition is met.

The while loop- This loop runs as long as a certain condition is true. It is useful when you want to execute a block of code until a condition is met, and you don't know how many times the loop will run.

The for loop- This loop is used to iterate over a collection of items such as arrays, vectors, or ranges. It is useful when you want to execute a code block for each item in a collection.

So, which loop to use depends on what you are trying to accomplish. Use the loop if you need to execute a code block indefinitely until a certain condition is met. If you need to execute a block of code a fixed number of times or until a certain condition is met, use the for or while loop.

Summary

In this article, we learn how to use loops in Rust; In the above, we learn all three types of loops in Rust programming language with the help of examples, output, and proper explanation. Lops are very. It has almost every programming language; all the best for the future. 

FAQs

Q-1 What is the difference between a loop and a while loop in Rust?

A- A loop in Rust runs the code block endlessly until a break statement is reached, while a while loop runs the code block repeatedly as long as the condition stated in the loop header is true.

Q- 2 Can I use a loop without an endpoint in Rust?

A- Yes, using the loop keyword, you can use a loop without an endpoint in Rust. However, you must include a break statement in your code block to exit the loop at some point; otherwise, the loop will run indefinitely.

Q- 3 Can I use nested loops in Rust?

A- Yes, you can use nested loops in Rust by simply including one loop inside the code block of another loop.


Similar Articles