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
- 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…
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
-
-
-
-
-
-
-
(Locked)
Conditional execution3m 38s
-
(Locked)
Multiple conditions3m 45s
-
(Locked)
Conditional assignment3m 41s
-
(Locked)
Loops3m 45s
-
(Locked)
While loops4m 55s
-
(Locked)
For loops6m 35s
-
(Locked)
Nested loops2m 51s
-
(Locked)
Challenge: Max, min, mean1m 13s
-
(Locked)
Solution: Max, min, mean2m 25s
-
(Locked)
-
-
-
-
-
-
-
-
-
-
-
-