3 ms·
> The rule of thumb for me is `thiserror` for libraries, `anyhow` for executables. And as the executable gets larger and refactored, some of the "executable" c
by zvrba 6y ago
> The rule of thumb for me is `thiserror` for libraries, `anyhow` for executables.
And as the executable gets larger and refactored, some of the "executable" code will inevitably become "library" code. What then?
- lpghatguy 6y agoThere's nothing stopping you from continuing to use anyhow in a library, it just doesn't produce error types with much structure. You can continue returning `anyhow::Error`, wrap it into a newtype error for your library, or refactor to use thiserror. Thanks to the `?` operator and Rust's type aliases, you can get away with very few code changes to switch between these!
- masklinn 6y ago> And as the executable gets larger and refactored, some of the "executable" code will inevitably become "library" code. What then? Then that is migrated to `thiserror` (or something bespoke) at the same time as it's migrated to the library context. The library / executable is the delineation between providing a Rust-level API for third parties versus consuming such APIs. If you're providing a Rust API, you want to provide precise error so that the user is able to precisely target and handle errors if they need to. If you're only consuming Rust APIs, then you want to precisely handle some of the errors you get, and just chuck the rest over the side.