---
title: "Option and Result | Bitterless Rust"
description: "\"Something or nothing\" and \"success or failure\" Read Option and Result | Bitterless Rust, an essay by ubugeeei on wtrclred."
last_updated: "2026-11-15"
canonical_url: "https://wtrclred.vercel.app/en/books/bitterless-rust/07"
language: "en"
---

# Option and Result | Bitterless Rust

Turn on watercolor background effects. * May affect performance, battery life, and temperature on some devices.

### Table of Contents

- [Option: Something or Nothing](#option-something-or-nothing)
- [How to Handle Option](#how-to-handle-option)
- [unwrap: "It's definitely there!"](#unwrap-it-s-definitely-there)
- [expect: unwrap with a Message](#expect-unwrap-with-a-message)
- [match: Handle It Safely](#match-handle-it-safely)
- [if let: When You Only Care About One Case](#if-let-when-you-only-care-about-one-case)
- [Result: Success or Failure](#result-success-or-failure)
- [You Can unwrap Result Too](#you-can-unwrap-result-too)
- [The ? Operator: Propagate Errors Upward](#the-operator-propagate-errors-upward)
- [Common Patterns](#common-patterns)
- [Summary](#summary)

## Option: Something or Nothing

 Rust has no `null`. Instead, it uses `Option`:

 

```rust
  enum   Option  <  T  >   { 
     Some  (  T  )  ,    // there's a value 
     None  ,       // there's no value 
 }
```

> means "any type." Option means "maybe an i32, maybe nothing." Option means "maybe a String, maybe nothing."

 

```rust
  fn   find_even  (  numbers  :   Vec  <  i32  >  )   -  >   Option  <  i32  >   { 
     for   n   in   numbers   { 
         if   n   %   2   =  =   0   { 
             return   Some  (  n  )  ; 
         } 
     } 
     None 
 } 

 fn   main  (  )   { 
     let   nums   =   vec  !  [  1  ,   3  ,   4  ,   7  ]  ; 
     let   result   =   find_even  (  nums  )  ; 

     match   result   { 
         Some  (  n  )   =  >   println  !  (  "found: {}"  ,   n  )  , 
         None   =  >   println  !  (  "not found"  )  , 
     } 
 }
```

## How to Handle Option

### unwrap: "It's definitely there!"

 

```rust
  fn   main  (  )   { 
     let   x  :   Option  <  i32  >   =   Some  (  42  )  ; 
     let   value   =   x  .  unwrap  (  )  ;   // 42 
     println  !  (  "{}"  ,   value  )  ; 
 }
```

 If you `unwrap()` a `None`, the program crashes (panics). Only use this when you're sure it's `Some`.

### expect: unwrap with a Message

 

```rust
  fn   main  (  )   { 
     let   x  :   Option  <  i32  >   =   Some  (  42  )  ; 
     let   value   =   x  .  expect  (  "value should exist"  )  ; 
     println  !  (  "{}"  ,   value  )  ; 
 }
```

 Same as `unwrap`, but you get to specify the crash message.

### match: Handle It Safely

 

```rust
  fn   main  (  )   { 
     let   x  :   Option  <  i32  >   =   Some  (  42  )  ; 

     match   x   { 
         Some  (  v  )   =  >   println  !  (  "got: {}"  ,   v  )  , 
         None   =  >   println  !  (  "nothing"  )  , 
     } 
 }
```

### if let: When You Only Care About One Case

 

```rust
  fn   main  (  )   { 
     let   x  :   Option  <  i32  >   =   Some  (  42  )  ; 

     if   let   Some  (  v  )   =   x   { 
         println  !  (  "got: {}"  ,   v  )  ; 
     } 
 }
```

 Handy when you want to do nothing for `None`.

## Result: Success or Failure

 `Result` represents whether an operation succeeded or failed:

 

```rust
  enum   Result  <  T  ,   E  >   { 
     Ok  (  T  )  ,     // success (value is T) 
     Err  (  E  )  ,    // failure (error is E) 
 }
```

 

```rust
  fn   divide  (  a  :   f64  ,   b  :   f64  )   -  >   Result  <  f64  ,   String  >   { 
     if   b   =  =   0.0   { 
         Err  (  String  :  :  from  (  "division by zero"  )  ) 
     }   else   { 
         Ok  (  a   /   b  ) 
     } 
 } 

 fn   main  (  )   { 
     match   divide  (  10.0  ,   3.0  )   { 
         Ok  (  result  )   =  >   println  !  (  "result: {}"  ,   result  )  , 
         Err  (  e  )   =  >   println  !  (  "error: {}"  ,   e  )  , 
     } 

     match   divide  (  10.0  ,   0.0  )   { 
         Ok  (  result  )   =  >   println  !  (  "result: {}"  ,   result  )  , 
         Err  (  e  )   =  >   println  !  (  "error: {}"  ,   e  )  , 
     } 
 }
```

## You Can unwrap Result Too

 

```rust
  fn   main  (  )   { 
     let   result   =   divide  (  10.0  ,   3.0  )  ; 
     let   value   =   result  .  unwrap  (  )  ;   // 3.333... 
     println  !  (  "{}"  ,   value  )  ; 
 }
```

 If you `unwrap()` an `Err`, it panics. Same rules as `Option`.

## The ? Operator: Propagate Errors Upward

 Using `?` inside a function returns the `Err` to the caller automatically:

 

```rust
  fn   calc  (  a  :   f64  ,   b  :   f64  ,   c  :   f64  )   -  >   Result  <  f64  ,   String  >   { 
     let   ab   =   divide  (  a  ,   b  )  ?  ;    // if Err, return immediately 
     let   result   =   divide  (  ab  ,   c  )  ?  ; 
     Ok  (  result  ) 
 } 

 fn   divide  (  a  :   f64  ,   b  :   f64  )   -  >   Result  <  f64  ,   String  >   { 
     if   b   =  =   0.0   { 
         Err  (  String  :  :  from  (  "division by zero"  )  ) 
     }   else   { 
         Ok  (  a   /   b  ) 
     } 
 } 

 fn   main  (  )   { 
     match   calc  (  100.0  ,   5.0  ,   2.0  )   { 
         Ok  (  v  )   =  >   println  !  (  "result: {}"  ,   v  )  , 
         Err  (  e  )   =  >   println  !  (  "error: {}"  ,   e  )  , 
     } 
 }
```

 `?` means "if Ok, extract the value; if Err, return that Err from this function immediately." Can only be used in functions that return `Result`.

## Common Patterns

 Many standard library functions return `Result`:

 

```rust
  fn   main  (  )   { 
     // String to number (might fail) 
     let   num  :   Result  <  i32  ,   _  >   =   "42"  .  parse  (  )  ; 
     println  !  (  "{}"  ,   num  .  unwrap  (  )  )  ;   // 42 

     let   bad  :   Result  <  i32  ,   _  >   =   "hello"  .  parse  (  )  ; 
     println  !  (  "{}"  ,   bad  .  is_err  (  )  )  ;   // true 
 }
```

> _ means "let the compiler figure out the type."

## Summary

 Type
 Meaning
 Variants

 `Option `
 Something or nothing
 `Some(T)` / `None`

 `Result `
 Success or failure
 `Ok(T)` / `Err(E)`

 How to handle them:

 Approach
 When to use

 `match`
 When you want to safely handle both cases

 `if let`
 When one case is enough

 `unwrap()` / `expect()`
 When you're sure it's Some/Ok

 `?`
 When you want to propagate errors to the caller

 Congrats. The Rust fundamentals are done.
Starting next chapter, we'll use everything you've learned to build a four-operation calculator.

 [← Previous The Dynamic Ones](/en/books/bitterless-rust/06)

 [Next → Practice: Parse and Display a Number](/en/books/bitterless-rust/08)

[← Back to book](/en/books/bitterless-rust)

## Sitemap

- [Browse every page on wtrclred](https://wtrclred.vercel.app/sitemap.md)
