Rust errors without dependencies
4 months ago
- #Error Handling
- #Rust
- #Programming
- Rust error handling is complex due to composability and lack of a 'blessed way' from the community.
- Popular libraries like anyhow, thiserror, and eyre are recommended, but the author prefers using the standard library for security and adaptability reasons.
- Security concerns include limiting third-party code to avoid supply chain disasters, similar to issues seen in NPM.
- Adaptability benefits come from using the standard library, which is universally familiar and well-vetted.
- Rust's error handling differs from the try-catch paradigm, emphasizing return codes for composability and safety, especially in embedded systems.
- Example code demonstrates the dangers of unwrap and the importance of proper error handling.
- The author argues that unwrap is acceptable when errors are guaranteed not to occur, but developers must ensure correctness.
- A detailed example shows how to write custom error enums in Rust, improving error messages with context and location data.
- Comparison with NodeJS highlights Rust's advantage in forcing immediate error handling, preventing crashes from malformed input.
- Issues with Rust error handling include verbosity and the need for cultural enforcement of best practices.
- The author calls for better documentation and community standards to improve Rust's error handling ecosystem.