From the course: First Look: Rust

Unlock the full course today

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

Control flow: While loops

Control flow: While loops - Rust Tutorial

From the course: First Look: Rust

Start my 1-month free trial

Control flow: While loops

- [Instructor] It's often useful to execute a block of code more than once. For this task, let's convert several loops. A loop runs through the code inside the loop body to the end, and then starts immediately back at the beginning. In order to evaluate a condition within a loop, we use the while expression. To loop through a collection, we use the for expression. To experiment with loops, let's make a new project and call it loops. (typing) Let's open up our main.rs file here. So, to use the loop in Rust, we have a keyword and the keyword is loop, please say it forward, right? So the loop is followed by the calibresys, which is the body of the loop, which gets executed multiple times. So now let's go ahead and write a statement here. (typing) So I close it off with a semicolon, save the file. So let me cd into the loops project, and on the project. (typing) So as you can see, again gets printed multiple times. To stop this, use control + c, so if you're a mac, just hit command + c…

Contents