Skip to content

Option Type

Medium — good to knowGeneral Dev

ELI5 — The Vibe Check

Option type is the type system's way of saying 'this might have a value, or it might be empty, and you MUST handle both cases.' It's like a gift box that might be empty — you have to open it and check before using what's inside.

Real Talk

A sum type that represents a value that may or may not exist, typically with variants Some(T) and None (Rust), Just(a) and Nothing (Haskell), or Optional (Swift/Java). Option types replace null references with a type-safe alternative that the compiler forces you to handle explicitly.

Show Me The Code

// Rust
fn find_user(id: u32) -> Option<User> {
    users.get(&id).cloned()
}

match find_user(42) {
    Some(user) => println!("Found: {}", user.name),
    None => println!("User not found"),
}

When You'll Hear This

"Use Option instead of null — the compiler will force you to handle the missing case." / "Option types eliminate entire categories of null pointer exceptions."

Made with passive-aggressive love by manoga.digital. Powered by Claude.