Rust Control Structures and How to Use Them

Control structures are a programming construct that allows you to control the flow of execution in your programs. Control structures let you specify instructions to run only if certain conditions are met.

Rust’s control structures are crucial for managing program flow, enabling efficient code execution while simplifying complex tasks into smaller, reusable components.

4

Rust Conditional Statements

Conditional statements are constructs that allow you to run code based on conditions. Conditional statements come in handy for decision-making since the program’s execution depends on whether the condition evaluates totrueorfalse. Rust providesif,else, andmatchstatements for decision-making.

In Rust programs, theifstatement tests whether a certain condition evaluates to true. If it does, the program runs the associated block of code. If the condition evaluates false, the program skips that block of code and moves on to the next statement or runs theelsestatement block if there’s any.

Rust code that interacts with files

Thematchstatement is a powerful control flow construct that allows a program to match values against a series of patterns and execute code based on the matching pattern.

Rust’s if Statements

You’ll declareifstatements in your Rust programs with theifkeyword followed by a condition:

Here’s an example of how you can use an if statement in your Rust programs:

Rust match statement example result

Thexvariable holds a 32-bit integer, and the if statement checks if the value of x is greater than ten before running the block of code that prints it.

Rust’s else Statements

You’ll use theelsekeyword to execute a block of code when you anticipate that anifstatement would evaluate as false.

Here’s an example wherexisn’t greater than 10, theifstatement evaluatesfalse, and anelsestatement runs.

Rust loop loop result

Sincexis 5 and 5 isn’t greater than 10, the program skips theifblock and executes theelseblock.

Rust’s match Statements

You’ll use thematchkeyword for complex decision-making to check a series of patterns and execute code based on pattern matches. Match statements are similar toswitch statements in C#, Go, and C++.

Here’s the structure of a Rust match statement:

Rust programming language logo

Here’s how you can use match statements in your programs:

Thegradevariable is a character, and thematchstatement checks for which of the characters evaluates to the value of the grade variable before executing the code after the => operator. You can use the underscore (_) pattern to match values that don’t match other patterns (the default pattern).

Loops in Rust

Loops are a fundamental construct used for repetitive tasks likeweb scrapingand other automated actions. Rust provides different types of loops, includingwhileloops,forloops, and thelooploop.

Rust’s while Loops

While loops repeat a block of code as long as a specified condition evaluates true. Before specifying the condition, you’ll specify while loops in Rust with thewhilekeyword.

The condition should be a boolean expression that determines the loop’s continuation. When the condition evaluates false, the loop exits.

Here’s an example of a Rust while loop that prints numbers from one to five.

The while loop in themainfunction traverses through the numbers one through five while incrementing theivariable by one untilivariable is greater than five, where the loop terminates.

The loop Loop

Theloopkeyword creates an infinite loop until you state an exit with thebreakkeyword.

The code in theloopblock will keep executing until the loop encounters abreakkeyword.

Here’s an example of using thelooploop to print numbers one through five before specifying thebreakkeyword for the exit.

Themainfunction executes the loop, and theifstatement increments theivariable. Theifstatement specifies the loop termination when theivariable exceeds five.

Rust’s for Loops

In Rust, for loops iterate through a range or a collection of values. You’ll use theforkeyword to start a for loop, before specifying the range or collection it acts on.

Therangeis an expression that evaluates to a sequence of values, and the variable is a variable that takes on each value in the sequence in turn. The code block will execute once for each value in the sequence.

Here’s an example of a for loop that prints values ranging from one through ten.

The loop iterates through the values from 1 to 10. On each iteration, the variable (i) holds the next value, that theprintln!macro then prints out.

You can use thecontinuekeyword to skip values in for-loops. Here’s how you can skip the even numbers when operating on a range:

Theifstatement uses thecontinuekeyword to specify that the loop should skip numbers divisible by two.

Additionally, you can exit a for-loop with thebreakkeyword. The loop terminates on encountering thebreakkeyword.

Theifstatement specifies that the loop should terminate when theivariable equals five.

Use Rust’s Control Structures With Data Types to Master the Language

Rust provides these flexible control structures for operations on built-in and custom data types. You can use these control structures to operate on compound and composite data structures like arrays, vectors, struct and enums.

Rust also provides structs. Structs are data structures that group related values into a single object. They are similar to classes in object-oriented languages, and you’re able to even define methods on them.

Learn how you can use this custom data type to improve the structure of your Rust programs.

If an AI can roast you, it can also prep you for emergencies.

Who asked for these upgrades?

Tor spoiled me forever.

My foolproof plan is to use Windows 10 until 2030, with the latest security updates.

I gripped my chair the entire time—and then kept thinking about it when the screen turned off.

Technology Explained

PC & Mobile