| <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="github crates-io docs-rs"><meta name="keywords" content="rust, rustlang, rust-lang, anyhow"><title>anyhow - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../anyhow/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../anyhow/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate anyhow</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 1.0.71</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#reexports">Re-exports</a></li><li><a href="#macros">Macros</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</a></li><li><a href="#types">Type Definitions</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">anyhow</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/anyhow/lib.rs.html#1-683">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p><a href="https://github.com/dtolnay/anyhow"><img src="https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github" alt="github" /></a> <a href="https://crates.io/crates/anyhow"><img src="https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust" alt="crates-io" /></a> <a href="https://docs.rs/anyhow"><img src="https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" alt="docs-rs" /></a></p> |
| <br> |
| <p>This library provides <a href="struct.Error.html" title="Error"><code>anyhow::Error</code></a>, a trait object based error |
| type for easy idiomatic error handling in Rust applications.</p> |
| <br> |
| <h2 id="details"><a href="#details">Details</a></h2> |
| <ul> |
| <li> |
| <p>Use <code>Result<T, anyhow::Error></code>, or equivalently <code>anyhow::Result<T></code>, as |
| the return type of any fallible function.</p> |
| <p>Within the function, use <code>?</code> to easily propagate any error that implements |
| the <code>std::error::Error</code> trait.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>anyhow::Result; |
| |
| <span class="kw">fn </span>get_cluster_info() -> <span class="prelude-ty">Result</span><ClusterMap> { |
| <span class="kw">let </span>config = std::fs::read_to_string(<span class="string">"cluster.json"</span>)<span class="question-mark">?</span>; |
| <span class="kw">let </span>map: ClusterMap = serde_json::from_str(<span class="kw-2">&</span>config)<span class="question-mark">?</span>; |
| <span class="prelude-val">Ok</span>(map) |
| }</code></pre></div> |
| </li> |
| <li> |
| <p>Attach context to help the person troubleshooting the error understand |
| where things went wrong. A low-level error like “No such file or |
| directory” can be annoying to debug without more context about what higher |
| level step the application was in the middle of.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>anyhow::{Context, <span class="prelude-ty">Result</span>}; |
| |
| <span class="kw">fn </span>main() -> <span class="prelude-ty">Result</span><()> { |
| ... |
| it.detach().context(<span class="string">"Failed to detach the important thing"</span>)<span class="question-mark">?</span>; |
| |
| <span class="kw">let </span>content = std::fs::read(path) |
| .with_context(|| <span class="macro">format!</span>(<span class="string">"Failed to read instrs from {}"</span>, path))<span class="question-mark">?</span>; |
| ... |
| }</code></pre></div> |
| <div class="example-wrap"><pre class="language-console"><code>Error: Failed to read instrs from ./path/to/instrs.json |
| |
| Caused by: |
| No such file or directory (os error 2)</code></pre></div></li> |
| <li> |
| <p>Downcasting is supported and can be by value, by shared reference, or by |
| mutable reference as needed.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// If the error was caused by redaction, then return a |
| // tombstone instead of the content. |
| </span><span class="kw">match </span>root_cause.downcast_ref::<DataStoreError>() { |
| <span class="prelude-val">Some</span>(DataStoreError::Censored(<span class="kw">_</span>)) => <span class="prelude-val">Ok</span>(Poll::Ready(REDACTED_CONTENT)), |
| <span class="prelude-val">None </span>=> <span class="prelude-val">Err</span>(error), |
| }</code></pre></div> |
| </li> |
| <li> |
| <p>If using the nightly channel, or stable with <code>features = ["backtrace"]</code>, a |
| backtrace is captured and printed with the error if the underlying error |
| type does not already provide its own. In order to see backtraces, they |
| must be enabled through the environment variables described in |
| <a href="https://doc.rust-lang.org/std/backtrace/index.html#environment-variables"><code>std::backtrace</code></a>:</p> |
| <ul> |
| <li>If you want panics and errors to both have backtraces, set |
| <code>RUST_BACKTRACE=1</code>;</li> |
| <li>If you want only errors to have backtraces, set <code>RUST_LIB_BACKTRACE=1</code>;</li> |
| <li>If you want only panics to have backtraces, set <code>RUST_BACKTRACE=1</code> and |
| <code>RUST_LIB_BACKTRACE=0</code>.</li> |
| </ul> |
| <p>The tracking issue for this feature is <a href="https://github.com/rust-lang/rust/issues/53487">rust-lang/rust#53487</a>.</p> |
| </li> |
| <li> |
| <p>Anyhow works with any error type that has an impl of <code>std::error::Error</code>, |
| including ones defined in your crate. We do not bundle a <code>derive(Error)</code> |
| macro but you can write the impls yourself or use a standalone macro like |
| <a href="https://github.com/dtolnay/thiserror">thiserror</a>.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>thiserror::Error; |
| |
| <span class="attribute">#[derive(Error, Debug)] |
| </span><span class="kw">pub enum </span>FormatError { |
| <span class="attribute">#[error(<span class="string">"Invalid header (expected {expected:?}, got {found:?})"</span>)] |
| </span>InvalidHeader { |
| expected: String, |
| found: String, |
| }, |
| <span class="attribute">#[error(<span class="string">"Missing attribute: {0}"</span>)] |
| </span>MissingAttribute(String), |
| }</code></pre></div> |
| </li> |
| <li> |
| <p>One-off error messages can be constructed using the <code>anyhow!</code> macro, which |
| supports string interpolation and produces an <code>anyhow::Error</code>.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">return </span><span class="prelude-val">Err</span>(<span class="macro">anyhow!</span>(<span class="string">"Missing attribute: {}"</span>, missing));</code></pre></div> |
| <p>A <code>bail!</code> macro is provided as a shorthand for the same early return.</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">bail!</span>(<span class="string">"Missing attribute: {}"</span>, missing);</code></pre></div> |
| </li> |
| </ul> |
| <br> |
| <h2 id="no-std-support"><a href="#no-std-support">No-std support</a></h2> |
| <p>In no_std mode, the same API is almost all 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.</p> |
| <div class="example-wrap"><pre class="language-toml"><code>[dependencies] |
| anyhow = { version = "1.0", default-features = false }</code></pre></div> |
| <p>Since the <code>?</code>-based error conversions would normally rely on the |
| <code>std::error::Error</code> trait which is only available through std, no_std mode |
| will require an explicit <code>.map_err(Error::msg)</code> when working with a |
| non-Anyhow error type inside a function that returns Anyhow’s error type.</p> |
| </div></details><h2 id="reexports" class="small-section-header"><a href="#reexports">Re-exports</a></h2><div class="item-table"><div class="item-row"><div class="item-left import-item" id="reexport.format_err"><code>pub use <a class="macro" href="macro.anyhow.html" title="macro anyhow::anyhow">anyhow</a> as format_err;</code></div></div></div><h2 id="macros" class="small-section-header"><a href="#macros">Macros</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.anyhow.html" title="anyhow::anyhow macro">anyhow</a></div><div class="item-right docblock-short">Construct an ad-hoc error from a string or existing non-<code>anyhow</code> error |
| value.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.bail.html" title="anyhow::bail macro">bail</a></div><div class="item-right docblock-short">Return early with an error.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.ensure.html" title="anyhow::ensure macro">ensure</a></div><div class="item-right docblock-short">Return early with an error if a condition is not satisfied.</div></div></div><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Chain.html" title="anyhow::Chain struct">Chain</a></div><div class="item-right docblock-short">Iterator of a chain of source errors.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Error.html" title="anyhow::Error struct">Error</a></div><div class="item-right docblock-short">The <code>Error</code> type, a wrapper around a dynamic error type.</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Context.html" title="anyhow::Context trait">Context</a></div><div class="item-right docblock-short">Provides the <code>context</code> method for <code>Result</code>.</div></div></div><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.Ok.html" title="anyhow::Ok fn">Ok</a></div><div class="item-right docblock-short">Equivalent to Ok::<_, anyhow::Error>(value).</div></div></div><h2 id="types" class="small-section-header"><a href="#types">Type Definitions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="type" href="type.Result.html" title="anyhow::Result type">Result</a></div><div class="item-right docblock-short"><code>Result<T, Error></code></div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="anyhow" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |