4/5
# If let statement ## Example Execute the following command to run [`./solutions/examples/if_let.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/if_let/solutions/examples/if_let.rs) ```shell cargo run --example if_let ``` ## Exercises Exercises are in [`./exercises/src/lib.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/if_let/exercises/src/lib.rs) ### Exercise 1 ```rust pub fn unwrap_or_default(x: Option<u32>, v: u32) -> u32 { todo!(); } ``` Use the `if`, `let` syntax to extract the value stored inside a `Some`. Return the inner value. If `x` is `None`, then return the default value `v`. ## Test ```shell cargo test ```
Execute the following command to run ./solutions/examples/if_let.rs
Exercises are in ./exercises/src/lib.rs
Use the if
, let
syntax to extract the value stored inside a Some
.
Return the inner value.
If x
is None
, then return the default value v
.
A targeted guide to using `if let` for `Option` Handling - Learn to effectively use Rust's `if let` statement to conditionally extract values from `Option` types. You'll implement a function that returns the contained value of a `Some` or a specified default for `None`.
Previous lesson
Previous
Next lesson
Next
Give us feedback
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 July 11, 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 July 11, 2025