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 execution

Conditional execution - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Conditional execution

- [Instructor] The simple programs we've written thus far have all executed their instructions sequentially in order from start to finish, but there may be times when we want to change the order in which those instructions get executed based on something that happens when the program runs. Perhaps we don't want to execute certain parts of the code, or maybe we want to repeat a set of instructions multiple times. Rust has several mechanisms we can use to control the flow of a program's execution in those ways. If expressions let us control which code gets executed depending on conditions the program can evaluate at runtime. It's like choosing which path to take when you reach a fork in the road. To demonstrate that, let's write an if expression to conditionally execute some code based on the value of x, which is declared here on line two. We start a new if expression with the keyword if followed by the condition, which is an…

Contents