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.

Loops

Loops - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Loops

- [Narrator] There will often be times when we need our program to repeat an action by executing a block of code multiple times. And we can accomplish that using Loops. Rust has three primary types of Loop expressions that are commonly used. Named loop, while and for. That first one, simply named loop indicates an infinite loop that will repeat a block of code over and over again forever until you explicitly tell it to stop. To demonstrate that let's write a program that repeatedly increments the count variable that's initialized to zero on line two. To create a new loop, we'll use the loop keyword followed by a pair of curly braces to hold the block of code to repeat. Within that, we'll increment the count variable by one, and then print a message to display the new count value. When I run this program, the loop repeats that block of code over and over incrementing the count variable. And it will continue…

Contents