While in Rust 1.55

Find this useful? Support us: Star on GitHub 6
Category: Loop | Language: Rust 1.55

The while loop in Rust 1.55 is used to execute a block of code continuously as long as a specified condition is true. The syntax of the while loop in Rust is as follows:

while condition {
    // code to be executed
}

The condition can be a Boolean expression, a comparison, or any other expression that returns a Boolean value. The code inside the loop will continue to execute as long as the condition is true.

Here is an example of using while loop to print numbers from 1 to 10:

let mut i = 1;

while i <= 10 {
println!("{}", i);
i += 1;
}

In this example, the while loop is used to execute the block of code that prints the value of i on the console. The loop continues to execute as long as i is less than or equal to 10. Once i becomes 11, the condition becomes false, and the loop terminates.

Another example could be using while loop to find the factorial of a number:

let mut n = 5;
let mut fact = 1;

while n > 0 {
fact *= n;
n -= 1;
}

println!("Factorial is {}", fact);

In this example, we use while loop to calculate the factorial of a number. The loop continues to execute as long as n is greater than 0. In each iteration, we calculate the factorial by multiplying the fact variable by n, then decrement n by 1. The loop will continue until n becomes 0 and the factorial is calculated. Finally, we print the calculated factorial on the console.

In summary, the while loop in Rust is a powerful construct that allows you to execute a block of code continuously as long as a condition is true. You can use it for a wide range of programming tasks, from counting to searching for data in a collection, among other things.