Concept

while - Rust Programming

A common task in programming is to repeatedly execute a loop as long as a certain condition is met. When the condition is no longer met, the loop is halted by utilizing the "break" command. This behavior can be accomplished using a combination of loops, if statements, else statements, and breaks. However, Rust provides a specialized language construct, called a "while loop," that can perform this task with greater ease.

Here is an example of a while loop in Rust that repeatedly runs a block of code as long as the variable "i" is less than 5:

let mut i = 0; while i < 5 { println!("i is {}", i); i += 1; }

In this example, the while loop will continue to execute the code block that prints the value of "i" and increments it by 1, as long as the condition "i < 5" is true. Once "i" becomes greater than or equal to 5, the loop will exit and the program will continue to execute any code that comes after the loop.

0

1

Updated 2023-02-03

References


Tags

Object-Oriented Programming

Learn After