When Is It Acceptable to Use unwrap() in Rust?

Use unwrap() in Rust only when a missing value indicates a bug that should crash the program, otherwise handle errors gracefully.

Use unwrap() only when you are certain the Option or Result cannot be None or Err, typically in prototypes, tests, or when a failure indicates a logic bug that should crash the program. In production code where errors are recoverable, prefer match, if let, or unwrap_or_else to handle the error gracefully.

// Only use unwrap() when a failure is a bug that should crash the program
let first_sentence = novel.split('.').next().unwrap();

// Use unwrap_or_else for recoverable errors with a fallback
let score = scores.get(&team_name).copied().unwrap_or(0);