Seriously #rust #rustlang #rustdev #rustprogramming people:

Use `thiserror` in library crates! Only use `anyhow` in binary crates!

Don't give your users a hard time fighting against `anhyow::Error` when using your library!

#programmingadvice #programming

@musicmatze When starting out with an executable project and then extract functionality into a separate library, this is usually the main task I perform besides moving stuff around.

@musicmatze I'm pretty new to #rustlang and I haven't had the chance to use any of them.

Just in case, why is one encouraged over the other in some cases, and the other way around?

For me not to commit an error in the future, thanks! 😉

@sancas Nice that you ask! ❤️

There are two cases: Library crates and "Binary crates" (essentially: Applications).
In the case of an application, you _most of the time_ do no longer care about your error types. There has been an error while doing something? Nothing you can do about it, just show it to the user, log it, etc and carry on (or exit the application).

@sancas In case of libraries, you want to give the user of the lib the opportunity to care about that error your library produces. So you want to give them knowledge about what kind of error occured and then they can decide what to do with that. They might have a way to handle that error and continue operation!
@sancas `thiserror` helps you defining errors for libraries that you can return to your users.
`anyhow` is more of the former: Just encapsulate that error and then bubble it up to your main() function where you can show it to the user and then let the application exit. The actual error _type_ is no longer relevant here, because you're exiting anyways!
@sancas does that help?

@musicmatze oh, I think I get the point.

With `anyhow` you get a generic Error type which may be convenient and easy way within your own app (in an scenario like something either worked or not).

But with `thiserror` you get different types of errors (network, timeout, out of bounds...) which can help you understand what's happening inside the library and decide next actions on top of it.

I get it now, thanks! 🙌

@sancas exactly what you said! Glad I could help!  🚀
@musicmatze I just started using thiserror for error definition as I was getting tired implementing std::fmt::Display all the time... Also the stacking of errors with #[from] is super nice!