Idiomatic Rust propagates errors with ? inside functions returning Result, and handles locally with match or combinators like map_err.
The ? operator
fn read() -> Result<String, std::io::Error> {
let s = std::fs::read_to_string("file.txt")?;
Ok(s)
}
Important interview questions and answers
- Q: Exceptions in Rust?
A: No—recoverable errors use Result; panics for unrecoverable bugs.
Self-check
- What does
?do on Err? - When is panic appropriate?
Tip: Use panic! for logic bugs, Result for expected failures like I/O or parsing.
Interview prep
- Exceptions in Rust?
No—recoverable errors use
Resultand?;panic!is for unrecoverable bugs.