Option — value may or may not exist (no error info). Result — operation succeeded or failed (carries error info).
Option:
Some(T) or None..map(), .and_then(), .unwrap_or().1fn find_user(id: u32) -> Option<String> {2 match id {3 1 => Some("Alice".to_string()),4 2 => Some("Bob".to_string()),5 _ => None,6 }7}89// Chaining10let greeting = find_user(1)11 .map(|name| format!("Hello, {}!", name))12 .unwrap_or("User not found".to_string());
Result:
Ok(T) or Err(E).? operator propagates errors up the call stack.1use std::fs;23fn read_config(path: &str) -> Result<String, Box<dyn std::error::Error>> {4 let content = fs::read_to_string(path)?;5 Ok(content)6}78// Chaining with map_err9fn parse(s: &str) -> Result<i32, String> {10 s.parse::<i32>()11 .map_err(|e| format!("Parse error: {}", e))12}
When to use:
Option for: lookups, optional fields, nullable values, iterators.Result for: file I/O, network calls, parsing, database queries.Common mistakes:
unwrap() in production — can panic on None/Err.? or match.Option when the error reason matters — use Result.