tezvyn:

What is the difference between unwrap and expect on Option and Result?

AI-drafted, machine-checkedSource: doc.rust-lang.orgintermediate

It tests your Rust error-handling discipline and justified panics. Both extract values or panic, but expect adds a custom message. Use them only for unrecoverable invariant violations, not routine errors. A red flag is using them as lazy error propagation.

WHAT THIS TESTS: This question probes whether you understand the boundary between recoverable errors and unrecoverable bugs in Rust. It reveals if you know that Result and Option are meant for error propagation, not silent suppression, and whether you can articulate when crashing the process is actually the correct behavior. Interviewers also want to see that you grasp the ergonomic and philosophical distinction between a generic panic and one that carries context.

A GOOD ANSWER COVERS: A strong response states that unwrap and expect behave identically in the success case, returning the inner value from an Option or Result, and identically in the failure case, calling panic. The sole difference is that expect takes a string argument and includes it in the panic message, which makes debugging easier when the failure represents a violated invariant. You should then state that neither is appropriate for expected runtime failures like missing files or bad network responses; those should be handled with match, if let, or the question mark operator to propagate errors. The legitimate production uses are narrow: prototyping, tests, benchmarks, and cases where you can statically prove the variant is Ok or Some, such as a hardcoded config path that is bundled with the binary, or immediately after an is_some check that the compiler cannot track but you have logically guaranteed.

COMMON WRONG ANSWERS: A red flag is saying that expect is safer than unwrap because it has a message; both panic and both terminate the thread. Another red flag is claiming you should never use them in production under any circumstances; this signals dogmatism and ignores real scenarios like poisoned mutexes or static assets. Conversely, saying they are fine for any error because Rust is fast signals that you treat Rust like a scripting language and do not understand zero-cost error propagation. Finally, confusing the custom message in expect with error logging or user-facing diagnostics is a mistake; panic messages are for developers, not end users.

LIKELY FOLLOW-UPS: The interviewer may ask how you would refactor a codebase littered with unwrap calls, in which case you should mention replacing them with the question mark operator and introducing a custom error type or using anyhow for application code. They might also ask about unwrap_or and unwrap_or_default, which return fallback values instead of panicking, or about unwrap_unchecked, which is unsafe and skips the check entirely. Another common follow-up is how panics interact with threads, unwinding, and Drop implementations, or when to use panic versus abort for process termination.

ONE CONCRETE EXAMPLE: Imagine a web server that embeds its HTML templates at compile time with the include_str macro. At runtime, the binary must contain that string, so calling include_str and then parsing a known delimiter is guaranteed to succeed. Using expect here with a message like template delimiter missing is appropriate because failure means the build pipeline is broken, not that a user sent bad input. If instead you were reading a user uploaded file, you should return a Result and map the io error to an HTTP 400 or 500 response rather than crashing the worker thread with unwrap.

Read the original → doc.rust-lang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.