“Unlock the Magic of Rust: Mastering if-let, while-let, and let-else with Fun and Engaging Examples"

Chidozie C. Okafor
5 min readApr 8, 2023

As we delve into the magical world of `if-let`, `while-let`, and `let-else`, we’ll discover the power and elegance of Rust’s pattern matching capabilities. In this article, we’ll look at how these three features can help you simplify your code and make it more readable and maintainable. You’ll learn how to: through simple, child-friendly examples

  1. Use if-let to gracefully handle optional values, allowing you to perform actions with the values when they're available and handle empty cases with ease.
  2. Unleash the potential of while-let to repeatedly perform actions on values inside containers like bags or boxes, stopping only when the container is empty.
  3. Master let-else to bind variables when a pattern matches, or diverge with actions like breaking, returning, or panicking when it doesn't match.

By the end of this article, you’ll have a firm understanding of these three powerful Rust features, which will open up new possibilities in your code and make you a more effective and confident Rust programmer.

if-let

Let’s imagine we have a magical box that sometimes has a toy inside and sometimes it’s empty. We want to check if there’s a toy inside and play with it, but we don’t want to do anything if the box is empty.

In Rust, we use `if-let` to do this kind of thing. It allows us to check if there’s something inside a “box” (called an Option) and do something with it only if there’s

Let’s say we have a magical box called my_magical_box:

let my_magical_box = Some("toy");

We can use if-let to check if there's a toy inside and play with it:

if let Some(toy) = my_magical_box {
println!("Yay! There's a {} inside the box! Let's play!", toy);
}

If there is a toy, we will see the following message: “Yay! Inside the box is a toy! Let’s have some fun!” However, if there is nothing, we will not see any message.

Let’s look at another example. Assume we have a magical fruit basket that can either contain a fruit or be empty:

let magical_fruit_basket = Some("apple");

We’ll see if there’s a fruit: “Yummy! We discovered an apple! “Let’s eat!” However, if it is empty, we will not see any message.

Even if the box is empty, we sometimes want to do something. For that, we can use else with if-let:

if let Some(fruit) = magical_fruit_basket {
println!("Yummy! We found a {}! Let's eat it!", fruit);
} else {
println!("Oh no! The fruit basket is empty. Let's get more fruits!");
}

If the basket is empty, we will hear: “Oh no! The fruit basket is depleted. Let’s go get some more fruits!”

This is how `if-let` works in Rust! It allows us to check if something is inside a “box” and only do something with it if there is a value inside. When the box is empty, we can use else to do something.

while-let

Let’s talk about the `while-let` keyword in Rust as if it were a story. Assume you have a bag of candies and want to take them out one by one until the bag is empty.

In Rust, we use `while-let` to accomplish this. It allows us to keep doing something with the values contained within a “bag” (referred to as an Option) until the bag is empty.

Here’s an easy example. Assume we have a magical candy bag named my_magical_candy_bag:

let mut my_magical_candy_bag = Some(5);

This means we have 5 candies inside the bag. We can use while-let to take candies out of the bag one by one:

while let Some(candies) = my_magical_candy_bag {
if candies > 0 {
println!("We have {} candies left. Let's eat one!", candies);
my_magical_candy_bag = Some(candies - 1);
} else {
my_magical_candy_bag = None;
}
}

We update the number of candies left in the bag every time we eat one. When the bag is empty, we set it to None, indicating that it is empty, and the while-let loop ends.

Here’s another one. Assume we have a magical box containing the numbers 1 through 5:

let mut magical_numbers_box = vec![1, 2, 3, 4, 5];

We can use while-let to take numbers out of the box one by one and print them:

while let Some(number) = magical_numbers_box.pop() {
println!("We found the number {} in the magical box!", number);
}

This will print:

We found the number 5 in the magical box!
We found the number 4 in the magical box!
We found the number 3 in the magical box!
We found the number 2 in the magical box!
We found the number 1 in the magical box!

And that’s how while-let works in Rust! It helps us keep doing something with the values inside a "bag" or "box" until it's empty.

let-else

Consider a magical box that can contain a toy or be empty. You want to open the box and play with the toy inside, but if it’s empty, you want to do something else, like get a new toy.

To accomplish this, we can use the let-else keyword in Rust. It allows us to see if there is something inside a “box” (such as an Option), and if so, we can use it. We can do something else if the box is empty.

Here’s an easy example. Assume we have a magical box named my_magical_box:

let my_magical_box = Some("toy");

We can use let-else to check if there's a toy inside and play with it. If the box is empty, we can get a new toy:

let Some(toy) = my_magical_box else {
println!("Oh no! The box is empty. Let's get a new toy!");
};
println!("Yay! We found a {} in the box! Let's play!", toy);

We’ll see if there’s a toy: “Yay! We discovered a toy in the box! Let’s have some fun!” If it’s empty, however, we’ll see: “Oh no! The box is completely empty. Let’s go buy a new toy!”

This is how let-else works in Rust! It allows us to determine whether or not there is something inside a “box” and then use it if there is. We can do something else if the box is empty.

Finally, we’ve delved into the fascinating world of Rust’s pattern matching capabilities using if-let, while-let, and let-else. You should now have a firm grasp on how these features can improve your Rust programming experience by making your code more elegant, readable, and maintainable. You’ve unlocked the magic of Rust’s pattern matching and taken a significant step forward in your Rust journey thanks to our fun and engaging examples.

Remember to keep practicing and applying these concepts to real-world problems as you continue to explore and learn more about Rust. The expressiveness and safety features of Rust are what give it its power, and mastering these pattern matching tools will help you harness that power effectively. So go forth and let Rust’s magic inspire you to create even more amazing software!

follow me on

twitter, linkedin

--

--

Chidozie C. Okafor

Software Engineer & Backend Magician 🎩 | Python, Rust | TypeScript, Node.js | Golang | Kafka & GRPC