Skip to main content

11 docs tagged with "control-flow"

View all tags

`match` Expressions (Part 1): The Basics of Pattern Matching

We've seen how to use if-else and else if to make decisions in our code. However, when you have a long chain of else if statements, it can become clumsy. For more complex conditional logic, Rust has a powerful control flow construct called match. A match expression allows you to compare a value against a series of patterns and then execute code based on which pattern matches.

`match` Expressions (Part 2): Advanced Patterns

In the previous article, we learned the basics of match expressions. Now, we're going to explore some of the more advanced patterns that make match one of Rust's most powerful features. These patterns allow you to write even more expressive and concise conditional code.

Combining Loops and Conditionals

You've now learned about the two fundamental pillars of control flow: conditionals (if) and loops (loop, while, for). The real power comes when you start combining these two concepts to build more complex and interesting logic. In this article, we'll explore some common patterns for using if expressions inside loops.

Conditional Statements: `if-else if-else` (Part 2)

In the previous article, we learned the basics of if-else expressions. This is great for handling a single condition, but what if you have multiple conditions you want to check? For this, Rust provides the else if expression.

Conditional Statements: `if-else` expressions (Part 1)

Welcome to our fourth series! Now that you've built some complete console applications, it's time to dive deeper into one of the most fundamental concepts in programming: control flow. Control flow allows you to run different blocks of code based on certain conditions. The most common way to do this is with if expressions.

Iterating over Collections

In the previous article, we introduced the for loop and used it to iterate over a range of numbers. The true power of for loops, however, comes from their ability to iterate over collections like arrays, vectors, and more. This is the most common and idiomatic way to work with collections in Rust.

Loops: `for` loops and the `..` range operator

We've explored loop for infinite loops and while for conditional loops. Now, we'll look at the most common and often most powerful loop in Rust: the for loop. for loops are excellent for executing a block of code a specific number of times and for iterating over collections.

Loops: `while` loops

In the previous article, we learned how to create infinite loops with the loop keyword. While loop is useful, it's often more common to loop while a certain condition is true. For this, Rust provides the while loop.

The `if let` and `while let` Control Flow Constructs

We've seen how powerful the match expression is for pattern matching. However, sometimes a match can be a bit verbose, especially when you only care about one of the possible cases. For these situations, Rust provides a convenient shorthand: if let.

Using `if` in a `let` Statement

In the previous articles, we explored how if-else expressions can be used to control the flow of our program. Now, we're going to look at a particularly powerful feature of Rust: because if is an expression, we can use it on the right side of a let statement to conditionally assign a value to a variable.