From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

While loops

While loops - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

While loops

- The keyword while is used to create a while loop in Rust that will continuously repeat a block of code while a specific condition is true. To demonstrate that let's create a while loop that repeatedly increments, the count variable declared on line 2, until it reaches the value 10. We'll start with the keyword while followed by a condition. We want this loop to continue repeating while count is less than 10, then we'll add curly braces to enclose the block of code we'll increment the count variable and then print it's value. This loop, we'll start by evaluating the conditional expression count less than 10. And if that's true, it will execute the block of code. Then it loops around to check that condition again. And if it's still true, it will execute once more and so on. When I run this program, we can see that the process repeats until the count is incremented to 10. It prints that value. And then the loop checks the…

Contents