Rust unwrap Function
last modified February 19, 2025
In Rust, the unwrap
function is commonly used to handle Option
and Result
types. These types are used to represent values that may
or may not exist or operations that may fail. The unwrap
function
extracts the value from an Option
or Result
, but it
panics if the value is None
or an error. This tutorial explains how
to use unwrap
with examples.
Option Type
The Option
type is used to represent a value that may or may not
exist. It has two variants: Some(T)
for a value and None
for no value.
fn main() { let some_value = Some(5); let none_value: Option<i32> = None; println!("Some value: {}", some_value.unwrap()); println!("None value: {}", none_value.unwrap()); // This will panic }
This example demonstrates using unwrap
with Option
.
The second call to unwrap
will panic because the value is
None
.
Result Type
The Result
type is used to represent the outcome of an operation
that may fail. It has two variants: Ok(T)
for success and
Err(E)
for failure.
fn main() { let ok_result: Result<i32, &str> = Ok(10); let err_result: Result<i32, &str> = Err("Something went wrong"); println!("Ok result: {}", ok_result.unwrap()); println!("Err result: {}", err_result.unwrap()); // This will panic }
This example demonstrates using unwrap
with Result
.
The second call to unwrap
will panic because the result is an
error.
Unwrap with Default
The unwrap_or
function allows you to provide a default value if
the Option
is None
or the Result
is an
error.
fn main() { let some_value = Some(5); let none_value: Option<i32> = None; println!("Some value: {}", some_value.unwrap_or(0)); println!("None value: {}", none_value.unwrap_or(0)); }
This example uses unwrap_or
to provide a default value when the
Option
is None
.
Unwrap with Error Handling
The unwrap_or_else
function allows you to handle errors by
providing a closure that returns a default value.
fn main() { let ok_result: Result<i32, &str> = Ok(10); let err_result: Result<i32, &str> = Err("Something went wrong"); println!("Ok result: {}", ok_result.unwrap_or_else(|err| { println!("Error: {}", err); 0 })); println!("Err result: {}", err_result.unwrap_or_else(|err| { println!("Error: {}", err); 0 })); }
This example uses unwrap_or_else
to handle errors and provide a
default value.
Unwrap with Expect
The expect
function is similar to unwrap
, but it
allows you to provide a custom error message if the value is None
or an error.
fn main() { let some_value = Some(5); let none_value: Option<i32> = None; println!("Some value: {}", some_value.expect("Value is None")); println!("None value: {}", none_value.expect("Value is None")); // Panics }
This example uses expect
to provide a custom error message when
the Option
is None
.
In this article, we explored how to use the unwrap
function in
Rust to handle Option
and Result
types. We covered
basic usage, providing default values, handling errors, and custom error
messages. While unwrap
is convenient, it should be used with
caution as it can cause your program to panic.