Errors for Rusts
Error handling in Rust uses the Result type for recoverable failures and panics for bugs that should stop the program. The question mark operator propagates errors upward, and custom error types describe failures. Smart pointers like Box, Rc and RefCell cover heap allocation and shared or interior-mutable data.
Stage: Abstraction · Part of the Rust roadmap · Last reviewed 19 November 2026
What to learn in Errors
- Return Result from a function and propagate errors with ?
- Define a custom error type and implement Display
- Decide when unwrap or expect is acceptable and when it is not
- Allocate recursive data with Box
- Share data with Rc and mutate it through RefCell
Errors interview questions
When should you use panic! instead of returning a Result?
Use a panic for unrecoverable situations that signal a bug, such as a broken invariant, or in tests and quick prototypes. For failures a caller could reasonably handle, like missing files or bad input, return a Result so the caller decides.
What problem does Rc<RefCell<T>> solve and what is its risk?
Rc allows several owners of one value, and RefCell allows mutation checked at runtime instead of compile time. Together they give shared mutable data in single-threaded code. Breaking the borrow rules at runtime causes a panic, and cycles can leak memory.
Where Errors fits in the Rust roadmap
Errors belongs to the Abstraction stage (advanced level), which takes about 28 hours. Traits and errors.