2/5
# Pattern match ## Example Execute the following command to run [`./solutions/examples/match.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/pattern_match/solutions/examples/match.rs) ```shell cargo run --example match ``` ## Exercises Exercises are in [`./exercises/src/lib.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/pattern_match/exercises/src/lib.rs) ### Exercise 1 ```rust pub fn num_to_string(num: u32) -> String { todo!(); } ``` Convert `num` into `String`, `"one"` to `"three"`. Return `"other"` if `num` is greater than 3. ### Exercise 2 ```rust pub fn unwrap_or_default(x: Option<u32>, v: u32) -> u32 { todo!(); } ``` Extract the value wrapped in `Some`. If `x` is `None`, return the default value `v`. ## Test ```shell cargo test ```
Execute the following command to run ./solutions/examples/match.rs
Exercises are in ./exercises/src/lib.rs
Convert num
into String
, "one"
to "three"
.
Return "other"
if num
is greater than 3.
Extract the value wrapped in Some
. If x
is None
, return the default value v
.
An illuminating look at Pattern Match - Discover how Rust's `match` expressions provide elegant and powerful ways to control program flow based on patterns. You'll solidify your understanding by implementing number-to-string conversions and custom logic for `Option` types.
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