Expert-led training for your team
Title: Error Handling in Rust: A Comprehensive Guide

6 April 2023

Title: Error Handling in Rust: A Comprehensive Guide

Error handling is an important aspect of any programming language, and Rust is no exception. In this guide, we will explore the different types of errors in Rust, how to handle them, and best practices for error handling.

Types of Errors in Rust

Rust has two types of errors: recoverable and unrecoverable errors. Recoverable errors are those that a program can recover from, such as a file not found error. Unrecoverable errors, on the other hand, are those that cannot be recovered from, such as a stack overflow.

Recoverable Errors

Recoverable errors are represented by the Result type in Rust. The Result type is an enumeration with two variants: Ok and Err. The Ok variant represents a successful computation, while the Err variant represents an error.

Here's an example of using Result to read a file:


use std::fs::File; use std::io::{BufRead, BufReader}; fn read_file(file_path: &str) -> Result<Vec<String>, std::io::Error> { let file = File::open(file_path)?; let reader = BufReader::new(file); let mut lines = Vec::new(); for line in reader.lines() { lines.push(line?); } Ok(lines) }

In the example above, read_file function returns a Result<Vec<String>, std::io::Error> type. This means that the function can either return a vector of strings or an std::io::Error type.

The ? operator in Rust is shorthand for a match statement that checks if the Result variant is Ok or Err. If it's Err, it will return the Err value from the function.

Unrecoverable Errors

Unrecoverable errors are represented by the panic! macro in Rust. panic! is used when a program reaches a state that it cannot recover from. For example, if a program tries to access an index outside of an array's bounds, it will panic.

Here's an example of using panic! macro:


fn divide(dividend: f32, divisor: f32) -> f32 { if divisor == 0.0 { panic!("division by zero"); } dividend / divisor }

In the example above, the divide function will panic if the divisor is equal to 0.0.

Handling Errors in Rust

Handling errors in Rust is done using the match expression. The match expression takes an expression and matches it against patterns. If a pattern matches, the corresponding code block is executed.

Here's an example of using match expression to handle errors:


fn main() { let result = read_file("file.txt"); match result { Ok(lines) => { for line in lines { println!("{}", line); } }, Err(error) => { eprintln!("Error reading file: {}", error); } } }

In the example above, the read_file function is called with "file.txt" as an argument. The result is matched using the match expression. If the result is Ok, the lines are printed to the console. If the result is Err, an error message is printed to the console.

Best Practices for Error Handling

Here are some best practices for error handling in Rust:

1. Use Result for recoverable errors and panic! for unrecoverable errors

It's important to distinguish between recoverable and unrecoverable errors in Rust. Use Result for recoverable errors and panic! for unrecoverable errors.

2. Provide meaningful error messages

When handling errors, it's important to provide meaningful error messages to users. This will help users understand what went wrong and how to fix it.

3. Handle errors as close to the source as possible

Handle errors as close to the source as possible. This will make it easier to determine the cause of the error and how to fix it.

4. Use ? operator to propagate errors

The ? operator can be used to propagate errors up the call stack. This can help reduce boilerplate code and make error handling easier.

5. Use crates for specialized error handling

Rust has many crates for specialized error handling, such as the thiserror crate for custom error types and the anyhow crate for easy error handling.


In this comprehensive guide, we explored the different types of errors in Rust, how to handle them, and best practices for error handling. By following these best practices, you can write robust and reliable Rust programs that are easy to maintain and debug.

Official documentation for error handling in Rust can be found in the Rust Programming Language book [1] and the Rust Standard Library documentation [2].

[1] [2]

JBI Training offers a number of courses. Some of our most popular courses are found below. We can customize a course for your teams needs, for any training requirements or requests simply get in touch. 

About the author: Daniel West
Tech Blogger & Researcher for JBI Training

+44 (0)20 8446 7555

[email protected]



Copyright © 2023 JBI Training. All Rights Reserved.
JB International Training Ltd  -  Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS

Modern Slavery Statement & Corporate Policies | Terms & Conditions | Contact Us


Rust training course                                                                          React training course

Threat modelling training course   Python for data analysts training course

Power BI training course                                   Machine Learning training course

Spring Boot Microservices training course              Terraform training course

Kubernetes training course                                                            C++ training course

Power Automate training course                               Clean Code training course