Transform with Option::map()
Write a Rust function that takes an Option
Option::map() is a combinator that transforms the inner value of an Option using a closure. It applies the closure only if the Option is Some, wrapping the result back into Option automatically. If the Option is None, it returns None without calling the closure. This makes map() a safe and concise alternative to match statements.
The map() method is a cornerstone of Rust's functional approach to optional values. It composes well with other combinators like and_then(), unwrap_or(), and filter(). Using map() eliminates boilerplate match arms and makes code more readable by focusing on the transformation logic rather than the None-case handling.
Time complexity is O(1) as map() is a simple enum variant check and optional closure call. Space complexity is O(1). The method inlines to efficient conditional code without runtime overhead.
Edge cases include Some with zero (0 * 2 = 0, still Some), None input (returns None), negative values (correctly doubled), and ensuring the closure only runs on Some values, never on None.
Example Input & Output
Negative doubled
None propagates through map
Double the value inside Some
None stays None
Zero doubled is still zero
Algorithm Flow

Solution Approach
Best Answers
fn solution(val: Option<i32>) -> Option<i32> {
val.map(|x| x * 2)
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
