Option, Result and the ? operator in Rust
Learn Rust · Beginner · 12 min · After Structs, enums and match in Rust
How does Rust handle a value that may be missing or an operation that may fail?
1Guess
fn main() { for text in ["42", "x"] { match text.parse::<i32>() { Ok(n) => println!("number {n}"), Err(_) => println!("not a number"), } } }2Read
No null, no exceptions
Rust has no null and no exceptions. A value that may be missing is an
Option<T>:Some(value)orNone. An operation that may fail returns aResult<T, E>:Ok(value)orErr(error). Both are ordinary enums, somatchworks on them. The compiler makes you handle both cases before you can use the value inside.3Code
Working with Option
1 / 2fn first_word(text: &str) -> Option<&str> { text.split_whitespace().next() } fn main() { if let Some(word) = first_word("hello brave world") { println!("first word: {word}"); } let empty = first_word(" "); println!("{}", empty.unwrap_or("nothing")); let length = first_word("rust").map(|w| w.len()); println!("{length:?}"); }Read the program. What will it print?
4Read
The ? operator
Inside a function that returns
Result,?after a call does two things. OnOk(v)it gives youv. OnErr(e)it returns that error from your function at once. In a function that returnsOption, it returnsNonethe same way.unwrapandexpectpanic on the bad case instead. They are fine for experiments, not for real programs.5Code
Passing errors up with ?
1 / 2use std::num::ParseIntError; fn total(a: &str, b: &str) -> Result<i32, ParseIntError> { let x = a.parse::<i32>()?; let y = b.parse::<i32>()?; Ok(x + y) } fn main() { println!("{:?}", total("20", "22")); match total("20", "abc") { Ok(sum) => println!("sum {sum}"), Err(e) => println!("error: {e}"), } }Read the program. What will it print?
6Fix
fn main() { let n: i32 = "42".parse()?; println!("{n}"); }7Say it back
8Mission · 10 min
Write a program with a list of two &str values. Parse both as numbers with ?, and print their sum or the error. Try "7" and "8", then "7" and "x".
cargo new parse-sum cd parse-sum cargo run
Common questions
- What is the difference between Option and Result in Rust?
- Option<T> is for a value that may be missing: it is Some(value) or None. Result<T, E> is for an operation that may fail: it is Ok(value) or Err(error). Use Option when absence is normal and needs no reason, and Result when you want to say what went wrong.
- What does the ? operator do in Rust?
- The ? operator goes after an expression that returns a Result or an Option. If it holds a good value, you get that value. If it holds an error or None, the function returns early with it. So the function must itself return a compatible Result or Option, or the code does not compile.
- When should I use unwrap in Rust?
- unwrap and expect return the value, but they panic and stop the program on Err or None. They are fine in quick experiments, in tests, and when you can prove the value is there. In real code, prefer match, if let, unwrap_or or the ? operator, so bad input is handled instead of crashing the program.
Sources
- https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html (read 2026-12-03)
- https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html (read 2026-12-03)
- https://doc.rust-lang.org/std/option/enum.Option.html (read 2026-12-03)
Measured with rustc 1.100.1 (play.rust-lang.org, stable, edition 2024) on 2026-12-03
Published Dec 3, 2026 · Updated Dec 3, 2026