When handling errors using unwrap and expect, unwrap should only be used
when it is obvious that failure is not possible, otherwise expect should be
preferred.
Inside of libraries, panics must be avoided unless recovery is impossible.
Don't do:
let text = std::fs::read_to_string("test").unwrap();Do instead:
let text = std::fs::read_to_string("test").expect("failed to read 'test'");See this excerpt from the latest version of the Rust book:
In production-quality code, most Rustaceans choose
expectrather thanunwrapand give more context about why the operation is expected to always succeed. That way, if your assumptions are ever proven wrong, you have more information to use in debugging.