# `unwrap` and `expect` ## Example Execute the following command to run [`./solutions/examples/unwrap.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/unwrap/solutions/examples/unwrap.rs) ```shell cargo run --example unwrap ``` ## Exercises Exercises are in [`./exercises/src/lib.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/unwrap/exercises/src/lib.rs) ### Exercise 1 ```rust pub fn parse_and_add(a: &str, b: &str) -> u32 { todo!(); } ``` Parse `a` and `b` into `u32` and return the sum. Call `a.parse().expect("...")` to parse `a` into `u32` and unwrap the inner value. Call `expect` with the error message `"Failed to parse variable"`. Do the same for `b`. ### Exercise 2 ```rust pub fn unwrap_and_add(x: Option<u32>, y: Option<u32>) -> u32 { todo!(); } ``` Call `unwrap` to get the inner values of `x` and `y`. Return their sum. ### Test ```shell cargo test ```
unwrap and expectExecute the following command to run ./solutions/examples/unwrap.rs
Exercises are in ./exercises/src/lib.rs
Parse a and b into u32 and return the sum.
Call a.parse().expect("...") to parse a into u32 and unwrap the inner value.
Call expect with the error message "Failed to parse variable".
Do the same for b.
Call unwrap to get the inner values of x and y. Return their sum.
A practical guide to `unwrap` and `expect` - Learn how these Rust methods directly access values in `Option` and `Result`, panicking on `None`/`Err` and allowing custom messages with `expect`. You'll parse strings to `u32`s and sum `Option<u32>`s, putting `unwrap` and `expect` into action.
Previous lesson
Previous
Next lesson
Next
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on November 7, 2025
Rust Developer
Rust Programming BasicsDuration: 6min
Duration: 18min
Duration: 47min
Duration: 15min
Duration: 19min
Duration: 8min
Duration: 12min
Duration: 46min
Duration: 14min
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on November 7, 2025