Check Option with is_some()
Write a Rust function that takes an Option
Option
Unlike unwrap() which panics on None, is_some() and is_none() are completely safe and never panic. They are the preferred way to check Options when you only need to know if a value exists, not the actual value itself. This is a fundamental pattern in Rust programming.
Time complexity is O(1) as is_some() is a simple enum variant check. Space complexity is O(1). The method compiles to a single instruction at the machine level.
Edge cases include Some with zero (0 is still a valid value, is_some returns true), None (always returns false), and ensuring the function works with different inner types through monomorphization.
Example Input & Output
None has no value
Some(100) has a value
Some(0) still has a value
None again
Some(5) has a value
Algorithm Flow

Solution Approach
Check if an Option value is Some using the is_some() method. This returns true if the option contains a value, and false if it is None. It is a concise way to test for presence without pattern matching.
is_some() is the most readable way to check an Option. The inverse is is_none(). For extracting the value, use unwrap() (panics if None) or pattern matching.
Time complexity is O(1), space complexity is O(1).
Best Answers
fn solution(val: Option<i32>) -> bool {
val.is_some()
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
