Option — value may or may not exist (no error info). Result — operation succeeded or failed (carries error info).
Option:
Some(T) (value present) or None (value absent)..unwrap(), .unwrap_or(), .map(), .and_then().1fn find_user(id: u32) -> Option<String> {2 if id == 1 {3 Some("Alice".to_string())4 } else {5 None6 }7}89match find_user(2) {10 Some(name) => println!("Found: {}", name),11 None => println!("User not found"),12}
Result:
Ok(T) (success) or Err(E) (failure with error info).? operator propagates errors up the call stack.1use std::fs;23fn read_config() -> Result<String, std::io::Error> {4 let content = fs::read_to_string("config.toml")?;5 Ok(content)6}78fn main() {9 match read_config() {10 Ok(config) => println!("{}", config),11 Err(e) => eprintln!("Error: {}", e),12 }13}
When to use:
Option for: optional fields, lookups, iterator results, nullable values.Result for: file I/O, network calls, parsing, any fallible operation.Common mistakes:
unwrap() in production — can panic on None/Err.? or explicit matching.Option and Result — Result must carry an error type.