Rust Control Flow
Rust supports a variety of control flow statements that allow you to branch code based on certain conditions.
If..Else Statements
Let's construct a simple Rust program that checks your age, to see if you're old enough to drink (in the USA).
First, we declare an integer variable to hold an age.
Next, we use an if
statement to execute a block of code, depending on the value of the age
variable.
Using the >=
comparison operator, we check if age
is greater than, or equal to, 21.
We'll use the built-in Rust println!
macro to write some text to the terminal, but only if the age
variable is greater than, or equal to, 21.
#![allow(unused)] fn main() { let age = 21; if age >= 21 { println!("You can drink beer!"); } }
Since the age is indeed equal to 21, the comparison expression returns a boolean true
value, and the if
block is executed.
If you change the value of the variable to something less than 21, you will see that the text is not printed out.
#![allow(unused)] fn main() { let age = 19; if age >= 21 { println!("You can drink beer!"); } }
You can also include an else
block to execute in case the if
condition does not return true.
The else
block will match any condition that is not satisfied by the if
expression.
#![allow(unused)] fn main() { let age = 19; if age >= 21 { println!("You can drink beer!"); } else { println!("You need to wait a bit longer to drink alcoholic beverages.") } }
A more advanced rendition of the if
block is to use the else if
statement before the final else
block.
This allows you to check more conditions, before resorting to the else
block.
You can use else if
blocks without having an else
block as well, but it's usually a good idea to handle any edge cases with a catch-all else
block.
#![allow(unused)] fn main() { let age = 19; if age >= 21 { println!("You can drink beer!"); } else if age == 20 { println!("You are only one year away from legally drinking beer!") } else if age == 19 { println!("Hold on a couple more years"); } }
Loop Keyword
For an infinite loop, you can use the loop
keyword.
To exit out of the loop, you must implement your own logic to determine, under what condition, the loop should exit.
At the appropriate time, simply use the break
statement to exit out of the loop.
Similar to exiting a process, with a given exit code, you can also return a value from the break statement.
Let's build a simple loop example.
This example simply prints out a line of text upon every iteration.
The loop is infinite, so you would need to use CTRL + C
to cancel execution, or kill the process from another utility.
To avoid overloading the CPU, we will include a call to the std::thread::sleep()
function, which delays the loop iterations.
fn main() { loop { println!("This is a loop"); std::thread::sleep(std::time::Duration::from_millis(800)); } }
Now that we've create an infinite loop, let's forcibly break out of the loop, after a certain number of iterations.
We'll create a mutable integer variable called loop_count
, before the loop starts, and initialize it to 0.
This variable will track the number of loop iterations that occur.
We will also define a variable called max_loops
to indicate how many loop iterations we will allow the program to execute.
On every iteration, we need to increment the loop_count
variable by 1, using the +=
assignment operator.
Finally, we will create an if
block that causes the loop
to break
if the loop_count
exceeds a certain value.
fn main() { let mut loop_count = 0u8; let max_loops = 5u8; loop { if loop_count >= max_loops { break; } println!("This is iteration {}", loop_count); loop_count += 1; } }
For Loops
One of the most common constructs you'll find across different programming languages is the for
loop.
This type of loop construct allows you to perform an action a certain number of times, or "for each" input element.
For example, let's say you have a retail store, and need to discount each item's price by 10%.
We'll use the simple tuple-based struct to create a list of items inside of a Vec
type.
If you don't understand the Vec
type, just think of it as a dynamically-sized array / list of items for now.
#![allow(unused)] fn main() { }