dtolnay / anyhow

Flexible concrete Error type built on std::error::Error
Apache License 2.0
5.51k stars 147 forks source link

Anyhow ¯\_(°ペ)_/¯

github crates.io docs.rs build status

This library provides anyhow::Error, a trait object based error type for easy idiomatic error handling in Rust applications.

[dependencies]
anyhow = "1.0"

Compiler support: requires rustc 1.39+


Details


No-std support

In no_std mode, almost all of the same API is available and works the same way. To depend on Anyhow in no_std mode, disable our default enabled "std" feature in Cargo.toml. A global allocator is required.

[dependencies]
anyhow = { version = "1.0", default-features = false }

With versions of Rust older than 1.81, no_std mode may require an additional .map_err(Error::msg) when working with a non-Anyhow error type inside a function that returns Anyhow's error type, as the trait that ?-based error conversions are defined by is only available in std in those old versions.


Comparison to failure

The anyhow::Error type works something like failure::Error, but unlike failure ours is built around the standard library's std::error::Error trait rather than a separate trait failure::Fail. The standard library has adopted the necessary improvements for this to be possible as part of RFC 2504.


Comparison to thiserror

Use Anyhow if you don't care what error type your functions return, you just want it to be easy. This is common in application code. Use thiserror if you are a library that wants to design your own dedicated error type(s) so that on failures the caller gets exactly the information that you choose.


License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.