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

Conditional assignment - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

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…

Contents