How does Rust handle errors with Result and Option, and when is it appropriate to panic instead?

technical-conceptual · Junior level · software-engineering

What the interviewer is really asking

Probes whether the candidate distinguishes recoverable errors handled in the type system from unrecoverable panics, and knows idiomatic propagation.

What to say

What to avoid

Example answers

Strong: Result<T, E> represents something that can fail with Ok or Err, and Option<T> represents a value that might be missing with Some or None, so the compiler makes me handle both. To propagate I use the ? operator, which returns the error early if it's Err and otherwise gives me the value, keeping the code flat. I only panic for real bugs, like an invariant being broken, and return a Result for expected failures like a file not existing.

Weak: If something might fail you wrap it in a Result, and then you call .unwrap() to get the value out. If it errors it just crashes, which is usually fine because it tells you something went wrong. Option is similar but for nulls.

Want questions matched to your role? Paste a job title, job description, or CV and get a personalized set, or go Pro to unlock the full bank.