Introduction to Rust

Control Flow in Rust

Control flow constructs empower programmers to steer the execution of their code. This guide delves into the core tools that Rust provides for making decisions and iterating through tasks.

Conditional Statements: If, Else If, Else

Conditional statements are the bedrock of decision-making in any programming language. In Rust, they are both powerful and flexible:

If Statement

The if statement evaluates a condition and executes a code block if the condition is true.

let num = 10;

if num > 0 {
    println!("Number is positive.");
}

Else If Clause

The else if clause allows you to test multiple conditions sequentially.

let temperature = 25;

if temperature > 30 {
    println!("It's hot outside!");
} else if temperature > 20 {
    println!("It's warm.");
} else {
    println!("It's chilly.");
}

Else Clause

The else clause provides a default action when none of the preceding conditions are met.

let is_raining = true;

if is_raining {
    println!("Don't forget an umbrella!");
} else {
    println!("Enjoy the weather!");
}

Matching and Pattern Matching

Rust's match statement takes control flow to the next level by enabling pattern matching:

let grade = "B";

match grade {
    "A" => println!("Excellent!"),
    "B" | "C" => println!("Good."),
    "D" => println!("Passing."),
    _ => println!("Not a valid grade."),
}

In this example, the match statement compares the value of grade against various patterns and executes the corresponding code block for the first matching pattern. The _ serves as a catch-all for patterns not explicitly listed.

Looping with While and For

Loops allow repetitive tasks to be performed efficiently. Rust offers two fundamental loop constructs:

While Loop

The while loop continues to execute a block of code as long as a given condition remains true.

let mut count = 0;

while count < 5 {
    println!("Count: {}", count);
    count += 1;
}

For Loop

The for loop iterates over a range, collection, or data that implements the Iterator trait.

for number in 1..=5 {
    println!("Number: {}", number);
}

In this example, the for loop iterates through the range from 1 to 5 (inclusive) and prints each number.

In the upcoming sections, we'll delve into these control flow constructs with more detailed examples and insights, allowing you to harness their capabilities to craft efficient and expressive Rust programs.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board