# How do Option, Result and the ? operator work in Rust?
Source: https://ruststudio.org/community/t/126/how-do-option-result-and-the-operator-work-in-rust
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: error-handling, option, result | Views: 0

## Short answer
Rust has no null and no exceptions. A value that may be missing is an Option, either Some(value) or None. An operation that may fail returns a Result, either Ok(value) or Err(error). The ? operator returns the error to the caller early, and match or if let handles each case.

## Answer
## Answer
`Option<T>` and `Result<T, E>` are ordinary enums from the standard library. Because they are types, the compiler forces you to deal with the missing or failed case before you can use the value inside. That is the replacement for null pointer errors and for forgotten error codes.

```rust title=src/main.rs
use std::num::ParseIntError;

fn find_user(id: u32) -> Option<&'static str> {
    match id {
        1 => Some("Ada"),
        2 => Some("Bo"),
        _ => None,
    }
}

fn parse_and_double(text: &str) -> Result<i32, ParseIntError> {
    let n: i32 = text.trim().parse()?; // on error: return it to the caller
    Ok(n * 2)
}

fn main() {
    println!("{:?} {:?}", find_user(1), find_user(9));
    println!("{}", find_user(9).unwrap_or("nobody"));
    if let Some(name) = find_user(2) {
        println!("found {name}");
    }
    println!("{:?}", parse_and_double(" 21 "));
    println!("{:?}", parse_and_double("x"));
    match parse_and_double("x") {
        Ok(v) => println!("{v}"),
        Err(e) => println!("error: {e}"),
    }
}
```

```text
Some("Ada") None
nobody
found Bo
Ok(42)
Err(ParseIntError { kind: InvalidDigit })
error: invalid digit found in string
```

## How to use them
- `match` handles every case explicitly. `if let Some(x) = ...` handles one case and ignores the other.
- `unwrap_or(default)` supplies a fallback value. `unwrap_or_else(|| ...)` computes it only when needed.
- `?` on a `Result` gives the `Ok` value, or returns the `Err` from the current function at once. The function must itself return a `Result` (or `Option`, for `?` on an `Option`).
- `.map(...)` changes the inside of a `Some` or `Ok` and leaves `None` or `Err` alone.

## Common mistakes
- Calling `.unwrap()` on input that can really be missing. It panics with a message such as `called Option::unwrap() on a None value`. Reserve `unwrap` for cases that cannot fail, or for quick experiments, and use `expect("reason")` to leave a message for yourself.
- Using `?` in `main` without changing its return type. The error is E0277; the fix is `fn main() -> Result<(), Box<dyn std::error::Error>>`.
- Returning `Err(String)` everywhere. A small enum of your own error cases is easier to match on later.

## Related questions
### When should a function return an error and when should it panic?
Return an error for things that can go wrong at run time (bad input, missing file). Panic for bugs, meaning broken assumptions inside the program.
