Pybites Logo Rust Platform

Option Combinators

Easy +2 pts

In the previous exercise, you used match to handle Option. But matching every time gets verbose:

let name: Option<String> = get_name(); let upper = match name { Some(n) => Some(n.to_uppercase()), None => None, };

Rust provides combinators — methods that transform Options without explicit matching.

.map() — transform the inner value

let name: Option<String> = Some("alice".to_string()); let upper = name.map(|n| n.to_uppercase()); // Some("ALICE") let empty: Option<String> = None; let upper = empty.map(|n| n.to_uppercase()); // None …

Login to see the full exercise.