Rust-aurant Recipes: Art of Error Handling in Rust
Tags: rust, javascript, beginners, programming
Published: 2023-07-25 21:47:00

---
title: Rust-aurant Recipes: Art of Error Handling in Rust
published: true
date: 2023-07-25 21:26:48 UTC
tags: rust,javascript,beginner,programming
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/1*QQZJBj-2E3NliBZz6kBbSQ.png)
_Rust recipe banner_

Imagine yourself chopping, frying, and boiling with the accuracy of a Michelin star chef as you prepare dinner for your guests. The unexpected occurs suddenly. Your culinary creation begins to sizzle and smoke because you forgot to lower the heat! You must move quickly. However, when you reach for your go-to pan lid to douse the flames, it slips and clatters to the floor ineffectively.

In many programming languages like Javascript and Python, a “try/catch” strategy is used to deal with such errors. This is similar to scrambling to catch that flying pan lid before your kitchen degenerates into a hellish wasteland. It involves halting the execution of your code and attempting to handle the catastrophe that has occurred. Although it may come in handy, it can also turn your code into a confusing web of try-catch blocks that is challenging to read, debug, and maintain.

```
try {
    bakeCookies();
} catch (error) {
    console.log("The oven isn't working: ", error);
}
```

What if you were prepared with a fire extinguisher? One straightforward, clear solution that is simple to implement and prevents misunderstandings. That’s very similar to Go’s error handling strategy, which treats errors like values. It’s a straightforward idea that prevents your code from becoming an exception-handling rollercoaster. But it’s a little too trite. It doesn’t have a way to distinguish between different kinds of errors.

```
cookies, err := bakeCookies()
if err != nil {
    fmt.Println("The oven isn't working: ", err)
}
```

Rust is ready to flip your error-handling burger to perfection at this point with tongs in hand. It combines the expressivity of exceptions with the ease of treating errors as values, similar to how Go does it. The outcome? a special, strong error handling system that is like a well-stocked kitchen, ready to handle any culinary disaster. The dependable sous chefs you have by your side to help you with your coding, er, cooking are Rust’s `Option` and `Result` types.

```
#[derive(Debug)]
enum OvenError {
    NoHeat,
    Overheat,
    Unknown(String),
}

let attempt = bake_cookies();
match attempt {
    Ok(cookies) => {
        println!("Yummy, our cookies are ready: {:?}", cookies);
    },
    Err(error) => {
        match error {
            OvenError::NoHeat => println!("Oops! The oven isn't heating."),
            OvenError::Overheat => println!("Oh no! The oven is overheating."),
            _ => println!("Something else went wrong with the oven: {}", error),
        }
    }
}
```

Put on your chef’s hat, apron, and grab your coding spatula. Let’s take a tasty tour of Rust’s sophisticated and reliable error handling techniques. By the end of this delicious journey, you’ll be cooking up Rust code that handles errors with grace, efficiency, and a dash of creativity, just like a professional chef handles kitchen hiccups!

### Types of Errors in Rust: The Recipe & The Cooking Process

In Rust, we typically deal with two types of errors:

1. **Compile-Time Errors:** These resemble recipe-related errors. When Rust doesn’t comprehend what you’re asking it to do, they happen. Rust will detect errors such as misspelt keywords and closed brackets when you attempt to compile your programme.
2. **Runtime Errors:** These omissions resemble mistakes that can occur while cooking. When your programme is running, they happen when something goes wrong. Runtime errors will result, for instance, if your programme tries to access a file that doesn’t exist or divides a number by zero.

Now, let’s take a look at how Rust helps us deal with these runtime errors.

### The Rust Fridge: Option and Result

Let’s assume we are in the kitchen of the Rust-aurant. Our ingredients are kept in a refrigerator in the kitchen. For the availability (or lack thereof) of these ingredients in Rust, there are two types, and they are Option and Result.

#### 1. Option Type: Do We Have the Ingredient?

The Option type is like checking if we have a specific ingredient in our fridge. It's an enum that can take two values:

- Some(value): This means the ingredient is available.
- None: This means the ingredient is not available.

Let’s see it in action:

```
fn get_ingredient(ingredient: &str) -> Option<&str> {
    match ingredient {
        "tomatoes" if self.tomatoes_in_stock > 0 => Some("tomatoes"),
        "garlic" if self.garlic_in_stock > 0 => Some("garlic"),
        _ => None,
    }
}
```

In the get\_ingredient function, we check if the requested ingredient is available. If it is, we return Some("ingredient"). If it's not, we return None.

To handle the Option returned by get\_ingredient, we can use match or if let.

More on match: [Deep dive into match](https://medium.com/@doziestar/unlock-the-magic-of-rust-mastering-if-let-while-let-and-let-else-with-fun-and-engaging-e9e9877bf320)

```
match get_ingredient("tomatoes") {
    Some(tomatoes) => println!("{} are available, let's cook!", tomatoes),
    None => println!("We're out of tomatoes! Let's change the recipe."),
}
```

#### 2. Result Type: Successful Recipe or Kitchen Disaster?

The Result type in Rust is similar to Option. But in the event of an error, it offers more details about what went wrong. It’s like telling us if our recipe was a success or a complete failure in the kitchen. It’s an enum with two possible values:

- Ok(value): This means the operation was successful.
- Err(e): This means the operation failed, and e will contain information about what went wrong.

Let’s use it in our kitchen:

```
enum KitchenError {
    OutOfStock(String),
    NotEnoughSalt,
    BurnedFood,
}

fn cook_meal(meal: &str) -> Result<&str, KitchenError> {
    match meal {
        "pasta" if self.pasta_in_stock > 0 => Ok("pasta"),
        "pasta" => Err(KitchenError::OutOfStock("pasta".to_string())),
        _ => Err(KitchenError::OutOfStock(meal.to_string())),
    }
}
```

In the cook\_meal function, we try to cook a meal. If the meal is available, we return Ok("meal"). If it's not, we return Err(KitchenError::OutOfStock("meal")).

Handling Result is similar to handling Option:

```
match cook_meal("pasta") {
    Ok(meal) => println!("{} is ready, bon appétit!", meal),
    Err(KitchenError::OutOfStock(ingredient)) => println!("We're out of {}, let's change the menu.", ingredient),
    Err(_) => println!("Oops, something went wrong in the kitchen."),
}
```

### Kitchen Aids: unwrap(), expect(), and the ? Operator

Like a food processor or blender might make kitchen tasks easier, Rust also has some built-in methods for error handling. These are unwrap(), expect(), and the ? operator.

#### 1. unwrap(): The Brave Sous-Chef

The unwrap() method can be compared to a daring sous-chef who takes an ingredient without checking first. When called on an Option or Result, unwrap() will give you the value if it's Some or Ok. But if it's None or Err, the program will panic and stop, just like a chef who realizes the key ingredient is missing.

```
let ingredient = get_ingredient("tomatoes").unwrap(); // This code might panic if we don't have tomatoes!

let meal = cook_meal("pasta").unwrap(); // Also, this might panic if the pasta cooking failed!
```

#### 2. expect(): The Prepared Sous-Chef

The expect() method is similar to unwrap(), but it lets you provide an error message. This is like a sous-chef who checks the fridge and alerts you when an ingredient is missing.

```
let ingredient = get_ingredient("tomatoes").expect("We're out of tomatoes!"); // Will panic if we don't have tomatoes!

let meal = cook_meal("pasta").expect("The pasta cooking failed!"); // Will panic if the pasta cooking failed!
```

#### 3. The ? Operator: The Swift Sous-Chef

The ? operator in Rust is like a swift sous-chef who, when they can't find an ingredient, immediately tells the head chef. When called on an Option or Result, ? will return the value if it's Some or Ok. But if it's None or Err, it will return from the function early, effectively passing the error up to the caller.

```
fn prepare_dinner() -> Result<String, KitchenError> {
    let ingredient = get_ingredient("tomatoes")?;
    let meal = cook_meal("pasta")?;
    Ok(format!("Dinner is served: {} with {}", meal, ingredient))
}
```

In prepare\_dinner(), if get\_ingredient("tomatoes") or cook\_meal("pasta") returns Err, the ? operator will immediately return the error, effectively stopping the function and passing the error up to the caller.

And that’s it, fellow code chefs! You’ve enjoyed a lavish tour of the Rust-aurant’s busy kitchens and learned the sophisticated technique for preparing error-free code. We’ve looked at various approaches to error handling in cooking code, from dousing a sudden kitchen fire with a pan lid to having a reliable fire extinguisher by our side.

The powerful combination of treating errors as values and the expressiveness of exceptions, much like the secret ingredient that unites a dish, was however the speciality of the magical Rust-aurant. With `Option` and `Result` as our devoted sous chefs, we are not only prepared for when things go wrong but also equipped to comprehend and effectively communicate these hiccups, making our code robust, flavorful, and resilient.

Just like in a fine dining establishment, keep in mind that making mistakes is an opportunity for improvement rather than a catastrophe. By adding another dash of comprehension to our coding dish with each `Option` and `Result`, we are doing more than just handling errors. So, my friends, put your chef’s hats on high and keep practising the graceful art of error handling in Rust.

Let your Rust-aurant and the hearts of those who eat your programmes be filled with the aromas of effective, dependable code. Coding, like cooking, is ultimately about the journey rather than the end product — every mistake is an opportunity to learn something new and take a step towards excellence.

Happy coding and bon appetit until our next coding cook-off!

* * *