Result in Rust: another way of error handling
Tags: rust, result, beginners, enum
Published: 2024-03-01 18:16:56

I've just started learning Rust in 2 days. And the first thing I was surprised about was how Rust handles errors. I have used several different programming languages and I thought that the only way to handle errors or exceptions is catching them with [try/catch statement](https://www.w3schools.com/java/java_try_catch.asp). But you know what there is no try/catch at all in Rust. Yeah, that's true I swear!

## Traditional way to handle error/exception

In other programming languages like Java, Python, we usually use the [Exception Approach for handling errors](https://www.baeldung.com/java-exceptions). More specifically, we would throw exception or raise error when we want to signal the caller of our code that there is an error it needs to handle. And the caller would use try/catch statement to catch the error in order to handle it. That's the way of handling error that we all know I guess. But that's not true with Rust. So, how exactly does Rust handle error?

Java try/catch statement:
```java
try {
  //  Block of code to try
  myFunction();
}
catch(Exception e) {
  //  Block of code to handle errors
}
```

## An Result always returned even in case of error
Yes, I say that. No mistake or typo here. But first, you need to know what Result is. [Result in Rust](https://doc.rust-lang.org/std/result/) is a built-in enum. It has two variants: Ok (for normal case) and Err (in case of any error occurred during runtime).
In case of no error, the caller of your code would receive a Result object of Ok variant. And if there was an error occurred, the Result object would be an Err variant.
And the caller would use the [match statement](https://doc.rust-lang.org/rust-by-example/flow_control/match.html) to handle both variants.

Handling Result using match statement ([Guessing Game](https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html#handling-invalid-input)):
```rust
let result = my_function() //guess.trim().parse()
let guess: u32 = match result {
    Ok(num) => num,
    Err(_) => continue,
};
```

As you can see, Rust uses Result with Err variant to signal the caller of your code about error occurrences. The caller would handle the error using match statement. He could skip the error and retry if the error is [recoverable](https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html), otherwise he could stop the program from running immediately using [panic! marco](https://doc.rust-lang.org/book/ch09-01-unrecoverable-errors-with-panic.html).

## Summary
Error handling in Rust uses a different approach compared to other common programming languages like Java, Python. There is nothing like try/catch statement in Rust. Instead, Rust provides a built-in enum called Result which would carry the error objects for us to handle using match statement.

## What's next
Actually, there is another surprise to me about Rust, which is the concept of ownership. There's a complete new world for me to explore there. And that's what I would like to learn and write about next. So, stay tune!

## References

- [Guessing Game ](https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html#handling-invalid-input)
- [Error handling in Rust](https://doc.rust-lang.org/book/ch09-00-error-handling.html)