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 - Rust Tutorial
From the course: First Look: Rust
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Variables and mutability3m 15s
-
(Locked)
Scalar data types: Integers4m 2s
-
(Locked)
Scalar data types: Floats2m 10s
-
(Locked)
Compound data types: Tuples3m 50s
-
(Locked)
Compound data types: Arrays3m 55s
-
(Locked)
How functions work4m 12s
-
(Locked)
Return a value from a function3m 44s
-
(Locked)
Control flow: if/else6m 9s
-
(Locked)
Control flow: While loops3m 19s
-
(Locked)
Control flow: For loops2m 9s
-
-
-
-
-
-