Understanding Result Enum, unwrap(), and unwrap_or() in Rust
Raju Modi / March 02, 2025
2 min read •
Description
A beginner-friendly guide to Rust's Result enum, including unwrap() and unwrap_or() with examples.
Introduction
In Rust, handling errors safely is crucial. Rust provides the Result enum to represent success (Ok(value)) and failure (Err(error)). This blog will cover:
- What is the
Resultenum? - How to use
unwrap()andunwrap_or()safely - Examples demonstrating when
unwrap()panics and when it doesn’t
What is the Result Enum?
The Result<T, E> enum is used to handle operations that might fail. It has two variants:
enum Result<T, E> {
Ok(T), // Represents a success
Err(E), // Represents an error
}Example: Using Result
fn divide(a: i32, b: i32) -> Result<i32, &'static str> {
if b == 0 {
Err("Cannot divide by zero")
} else {
Ok(a / b)
}
}
fn main() {
let result = divide(10, 2);
match result {
Ok(value) => println!("Division successful: {}", value),
Err(error) => println!("Error: {}", error),
}
}unwrap() - Extracting Values from Result
The .unwrap() function extracts the value inside Ok(value). However, if the Result is Err, it panics and crashes the program.
✅ Example: unwrap() works without panic
fn main() {
let x: Result<i32, &str> = Ok(10);
let value = x.unwrap(); // Extracts 10 successfully
println!("Unwrapped value: {}", value);
}Output:
Unwrapped value: 10
❌ Example: unwrap() panics
fn main() {
let x: Result<i32, &str> = Err("Something went wrong!");
let value = x.unwrap(); // Panics and crashes
println!("Unwrapped value: {}", value);
}Output:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "Something went wrong!"'
unwrap_or() - Providing a Default Value
Instead of panicking, .unwrap_or(default_value) provides a fallback when Err occurs.
✅ Example: Preventing Panic with unwrap_or()
fn main() {
let x: Result<i32, &str> = Err("Something went wrong!");
let value = x.unwrap_or(-1); // Returns -1 instead of panicking
println!("Value: {}", value);
}Output:
Value: -1
Conclusion
- Use
unwrap()only when you are sure theResultisOk(value). - Use
unwrap_or(default_value)to avoid panics by providing a safe fallback. - Always handle
Resultproperly usingmatchor error-handling functions.
Rust makes error handling safe and predictable. Happy coding! 🚀