codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

21 / 58 topics
21Error Handling22Panic!
Tutorials/Rust/Error Handling
🦀Rust

Error Handling

Updated 2026-05-15
10 min read

Error Handling

Introduction

In Rust, error handling is a crucial aspect of writing robust and reliable programs. Unlike some other languages that use exceptions for error management, Rust employs a more explicit approach using the Result and Option types. This tutorial will guide you through understanding and implementing these types to handle errors effectively in your Rust applications.

Concept

Result Type

The Result type is an enum defined in the standard library that represents either success (Ok) or failure (Err). It is typically used for operations that can fail, such as file I/O or parsing data. The Result type has two variants:

  • Ok(T): Represents a successful operation with a value of type T.
  • Err(E): Represents an error with a value of type E.

Option Type

The Option type is another enum that represents the presence (Some) or absence (None) of a value. It is commonly used to handle cases where a value might be missing, such as searching for an element in a collection.

  • Some(T): Represents the presence of a value of type T.
  • None: Represents the absence of any value.

Examples

Using Result Type

Let's start by looking at how you can use the Result type to handle errors. Consider a function that attempts to parse an integer from a string:

Rust
1fn parse_integer(s: &str) -> Result<i32, std::num::ParseIntError> {
2 s.parse()
3}

In this example, the parse_integer function returns a Result<i32, std::num::ParseIntError>. If the parsing is successful, it returns Ok(i32), otherwise, it returns Err(std::num::ParseIntError).

To handle the result, you can use pattern matching with match or methods like unwrap, expect, and map.

Using match

Rust
1fn main() {
2 let result = parse_integer("123");
3
4 match result {
5 Ok(num) => println!("Parsed number: {}", num),
6 Err(e) => println!("Failed to parse integer: {}", e),
7 }
8}

Using unwrap

Rust
1fn main() {
2 let result = parse_integer("123");
3
4 // This will panic if the Result is an Err
5 let num = result.unwrap();
6 println!("Parsed number: {}", num);
7}

Using expect

Rust
1fn main() {
2 let result = parse_integer("123");
3
4 // This will panic with a custom message if the Result is an Err
5 let num = result.expect("Failed to parse integer");
6 println!("Parsed number: {}", num);
7}

Using Option Type

Now, let's look at how you can use the Option type. Consider a function that searches for a key in a map and returns its value:

Rust
1use std::collections::HashMap;
2
3fn find_value(map: &HashMap<String, i32>, key: &str) -> Option<i32> {
4 map.get(key).cloned()
5}

In this example, the find_value function returns an Option&lt;i32&gt;. If the key is found in the map, it returns Some(i32), otherwise, it returns None.

To handle the result, you can use pattern matching with match or methods like unwrap, expect, and map.

Using match

Rust
1fn main() {
2 let mut map = HashMap::new();
3 map.insert("one".to_string(), 1);
4
5 let result = find_value(&map, "one");
6
7 match result {
8 Some(value) => println!("Found value: {}", value),
9 None => println!("Key not found"),
10 }
11}

Using unwrap

Rust
1fn main() {
2 let mut map = HashMap::new();
3 map.insert("one".to_string(), 1);
4
5 let result = find_value(&map, "one");
6
7 // This will panic if the Option is None
8 let value = result.unwrap();
9 println!("Found value: {}", value);
10}

Using expect

Rust
1fn main() {
2 let mut map = HashMap::new();
3 map.insert("one".to_string(), 1);
4
5 let result = find_value(&map, "one");
6
7 // This will panic with a custom message if the Option is None
8 let value = result.expect("Key not found");
9 println!("Found value: {}", value);
10}

What's Next?

In this tutorial, we covered how to handle errors using the Result and Option types in Rust. In the next section, we will explore more advanced error handling techniques, including custom error types and the Panic! macro.

Stay tuned for more insights into building robust applications with Rust!


PreviousCratesNext Panic!

Recommended Gear

CratesPanic!