Code Logo

Check Option with is_some()

Published at25 Jul 2026
Rust Functions Easy 0 views
Like0

Write a Rust function that takes an Option and returns true if the option contains a value (is_some) or false if it is None. Use the is_some() method to perform this check safely without unwrapping.

Option is Rust's way of expressing that a value may or may not exist. It replaces null pointers from other languages. The is_some() method returns true if the Option is Some variant, and is_none() returns true if it's None. These methods are the safest way to check an Option without risking a panic.

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

Example 1
Input
None
Output
false
Explanation

None has no value

Example 2
Input
Some(100)
Output
true
Explanation

Some(100) has a value

Example 3
Input
Some(0)
Output
true
Explanation

Some(0) still has a value

Example 4
Input
None
Output
false
Explanation

None again

Example 5
Input
Some(5)
Output
true
Explanation

Some(5) has a value

Algorithm Flow

Recommendation Algorithm Flow for Check Option with is_some()
Recommendation Algorithm Flow for Check Option with is_some()

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.

fn solution(o: Option<i32>) -> bool {
    o.is_some()
}

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

rust - Approach 1
fn solution(val: Option<i32>) -> bool {
    val.is_some()
}