unwrap extracts the value from an Option or Result when it is present or successful; extraction failure panics. It can be convenient, but using it as a shortcut may hide failure paths and signal incomplete error handling. section 10.10 section 10.3
Choose an alternative that matches how the program should handle failure:
- Use expect to add a custom message; reserve it for cases where failure should stop the program, such as debugging or an expected exit. section 10.3
- Use map to transform a present or successful value, and and_then to chain operations that return Option or Result. section 10.3
- Use unwrap_or to supply a default, or ? to propagate a Result error up the call stack. section 10.3
unwrap is not always inappropriate: it may be reasonable when success is sufficiently certain. Otherwise, consider what should happen when a value is absent or an operation fails before choosing how to handle it. section 10.3
let successes: Vec<_> =
successes.into_iter().map(Result::unwrap).collect();
let failures: Vec<_> =
failures.into_iter().map(Result::unwrap_err).collect();
println!("successses={:?}", successes);
println!("failures={:?}", failures);
This iterator example unwraps successful parse results after successes and failures have already been partitioned. section 3.2.5




