Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Latest commit

 

History

History
32 lines (21 loc) · 850 Bytes

File metadata and controls

32 lines (21 loc) · 850 Bytes

Error Handling

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.

Example

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'");

Explanation

See this excerpt from the latest version of the Rust book:

In production-quality code, most Rustaceans choose expect rather than unwrap and 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.