Check Success with is_ok()
Write a Rust function that takes a Result
Result
The is_ok() method is complemented by is_err() which returns true for the Err variant. Both methods are zero-cost abstractions that compile to simple discriminant checks on the Result's enum tag. They are the preferred way to check Result types without unwrapping when only the success/failure status is needed.
Time complexity is O(1) as is_ok() is a simple enum variant check. Space complexity is O(1). The method compiles to a conditional instruction without any branching overhead beyond the enum tag check.
Edge cases include Ok with zero or default values (is_ok still returns true), Err with various error types (always returns false), and ensuring the method does not consume the Result so it can be used multiple times.
Example Input & Output
Ok with zero is still Ok
Err with short message
Ok with large value
Ok result returns true for is_ok
Err result returns false for is_ok
Algorithm Flow

Solution Approach
Check if a Result value is Ok (success) using the is_ok() method. Returns true if the result contains a success value, false if it contains an error. This is the simplest way to check success status without pattern matching.
is_ok() pairs with is_err() for checking failure. To extract the success value, use unwrap() (panics on Err) or expect() (custom panic message). For safe extraction, match on Ok/Err or use ok()/err() to convert to Option.
Time complexity is O(1), space complexity is O(1).
Best Answers
fn solution(val: Result<i32, String>) -> bool {
val.is_ok()
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
