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.
Conditional assignment - Rust Tutorial
From the course: Rust Essential Training
Conditional assignment
- [Instructor] When we build out complex if else structures with multiple possible paths of execution, it can create the potential for problems. Consider the example code shown here, which initializes the make X odd variable on line two with a value of true, and then declares a second variable X on line three, which is not initialized. That initialization is left to the if else structure on lines five through nine. If make X odd is true, then we assign it a value of one. Otherwise we'll assign it to two. At the end of the program, the print macro on line 11 displays the value of X. If I run this program, it works as we should expect. Since make X odd is set to true, we get an output message that X is one. Now, since make X odd is always true here, the else block on line seven will never get executed. So there's no harm in commenting that out, right? Let's try running that again. And now it fails to compile. The compiler…
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)
-
-
-
-
-
-
-
-
-
-
-
-