You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport" content="width=device-width, initial-scale=1.0"><metaname="generator" content="rustdoc"><metaname="description" content="List of all items in this crate"><title>List of all items in this crate</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><linkrel="stylesheet" href="../static.files/normalize-9960930a.css"><linkrel="stylesheet" href="../static.files/rustdoc-17e0aaed.css"><metaname="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="anyhow" data-themes="" data-resource-suffix="" data-rustdoc-version="1.97.0-nightly (f53b654a8 2026-04-30)" data-channel="nightly" data-search-js="search-b5634cc7.js" data-stringdex-js="stringdex-2da4960a.js" data-settings-js="settings-170eb4bf.js" ><scriptsrc="../static.files/storage-41dd4d93.js"></script><scriptdefersrc="../static.files/main-aa4d37ac.js"></script><noscript><linkrel="stylesheet" href="../static.files/noscript-f7c3ffd8.css"></noscript><linkrel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><linkrel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><bodyclass="rustdoc mod sys"><aclass="skip-main-content" href="#main-content">Skip to main content</a><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><ahref="#">All</a></h2></rustdoc-topbar><navclass="sidebar"><divclass="sidebar-crate"><h2><ahref="../anyhow/index.html">anyhow</a><spanclass="version">1.0.102</span></h2></div><divclass="sidebar-elems"><sectionid="rustdoc-toc"><h3><ahref="#macros">Crate Items</a></h3><ulclass="block"><li><ahref="#macros" title="Macros">Macros</a></li><li><ahref="#structs" title="Structs">Structs</a></li><li><ahref="#traits" title="Traits">Traits</a></li><li><ahref="#functions" title="Functions">Functions</a></li><li><ahref="#types" title="Type Aliases">Type Aliases</a></li></ul></section><divid="rustdoc-modnav"></div></div></nav><divclass="sidebar-resizer" title="Drag to resize sidebar"></div><main><divclass="width-limiter"><sectionid="main-content" class="content" tabindex="-1"><divclass="main-heading"><h1>List of all items</h1><rustdoc-toolbar></rustdoc-toolbar></div><h3id="structs">Structs</h3><ulclass="all-items"><li><ahref="struct.Chain.html">Chain</a></li><li><ahref="struct.Error.html">Error</a></li></ul><h3id="traits">Traits</h3><ulclass="all-items"><li><ahref="trait.Context.html">Context</a></li></ul><h3id="macros">Macros</h3><ulclass="all-items"><li><ahref="macro.anyhow.html">anyhow</a></li><li><ahref="macro.bail.html">bail</a></li><li><ahref="macro.ensure.html">ensure</a></li></ul><h3id="functions">Functions</h3><ulclass="all-items"><li><ahref="fn.Ok.html">Ok</a></li></ul><h3id="types">Type Aliases</h3><ulclass="all-items"><li><ahref="type.Result.html">Result</a></li></ul></section></div></main></body></html>
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport" content="width=device-width, initial-scale=1.0"><metaname="generator" content="rustdoc"><metaname="description" content="Equivalent to `Ok::<_, anyhow::Error>(value)`."><title>Ok in anyhow - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><linkrel="stylesheet" href="../static.files/normalize-9960930a.css"><linkrel="stylesheet" href="../static.files/rustdoc-17e0aaed.css"><metaname="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="anyhow" data-themes="" data-resource-suffix="" data-rustdoc-version="1.97.0-nightly (f53b654a8 2026-04-30)" data-channel="nightly" data-search-js="search-b5634cc7.js" data-stringdex-js="stringdex-2da4960a.js" data-settings-js="settings-170eb4bf.js" ><scriptsrc="../static.files/storage-41dd4d93.js"></script><scriptdefersrc="sidebar-items.js"></script><scriptdefersrc="../static.files/main-aa4d37ac.js"></script><noscript><linkrel="stylesheet" href="../static.files/noscript-f7c3ffd8.css"></noscript><linkrel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><linkrel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><bodyclass="rustdoc fn"><aclass="skip-main-content" href="#main-content">Skip to main content</a><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><ahref="#">Ok</a></h2></rustdoc-topbar><navclass="sidebar"><divclass="sidebar-crate"><h2><ahref="../anyhow/index.html">anyhow</a><spanclass="version">1.0.102</span></h2></div><divclass="sidebar-elems"><divid="rustdoc-modnav"></div></div></nav><divclass="sidebar-resizer" title="Drag to resize sidebar"></div><main><divclass="width-limiter"><sectionid="main-content" class="content" tabindex="-1"><divclass="main-heading"><divclass="rustdoc-breadcrumbs"><ahref="index.html">anyhow</a></div><h1>Function <spanclass="fn">Ok</span> <buttonid="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><spanclass="sub-heading"><aclass="src" href="../src/anyhow/lib.rs.html#650-652">Source</a></span></div><preclass="rust item-decl"><code>pub fn Ok<T>(value: T) -> <aclass="type" href="type.Result.html" title="type anyhow::Result">Result</a><T></code></pre><detailsclass="toggle top-doc" open><summaryclass="hideme"><span>Expand description</span></summary><divclass="docblock"><p>Equivalent to <code>Ok::<_, anyhow::Error>(value)</code>.</p>
2
+
<p>This simplifies creation of an <code>anyhow::Result</code> in places where type
3
+
inference cannot deduce the <code>E</code> type of the result — without needing
4
+
to write <code>Ok::<_, anyhow::Error>(value)</code>.</p>
5
+
<p>One might think that <code>anyhow::Result::Ok(value)</code> would work in such cases
6
+
but it does not.</p>
7
+
<divclass="example-wrap"><preclass="language-console"><code>error[E0282]: type annotations needed for `std::result::Result<i32, E>`
8
+
--> src/main.rs:11:13
9
+
|
10
+
11 | let _ = anyhow::Result::Ok(1);
11
+
| - ^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `E` declared on the enum `Result`
12
+
| |
13
+
| consider giving this pattern the explicit type `std::result::Result<i32, E>`, where the type parameter `E` is specified</code></pre></div></div></details></section></div></main></body></html>
<p>Use <code>Result<T, anyhow::Error></code>, or equivalently <code>anyhow::Result<T></code>, as
10
+
the return type of any fallible function.</p>
11
+
<p>Within the function, use <code>?</code> to easily propagate any error that implements
12
+
the <ahref="https://doc.rust-lang.org/nightly/core/error/trait.Error.html" title="trait core::error::Error"><code>std::error::Error</code></a> trait.</p>
.with_context(|| <spanclass="macro">format!</span>(<spanclass="string">"Failed to read instrs from {}"</span>, path))<spanclass="question-mark">?</span>;
35
+
...
36
+
}</code></pre></div><divclass="example-wrap"><preclass="language-console"><code>Error: Failed to read instrs from ./path/to/instrs.json
37
+
38
+
Caused by:
39
+
No such file or directory (os error 2)</code></pre></div></li>
40
+
<li>
41
+
<p>Downcasting is supported and can be by value, by shared reference, or by
42
+
mutable reference as needed.</p>
43
+
44
+
<divclass="example-wrap"><preclass="rust rust-example-rendered"><code><spanclass="comment">// If the error was caused by redaction, then return a
<p>If using Rust ≥ 1.65, a backtrace is captured and printed with the
52
+
error if the underlying error type does not already provide its own. In
53
+
order to see backtraces, they must be enabled through the environment
54
+
variables described in <ahref="https://doc.rust-lang.org/nightly/std/backtrace/index.html#environment-variables" title="mod std::backtrace"><code>std::backtrace</code></a>:</p>
55
+
<ul>
56
+
<li>If you want panics and errors to both have backtraces, set
57
+
<code>RUST_BACKTRACE=1</code>;</li>
58
+
<li>If you want only errors to have backtraces, set <code>RUST_LIB_BACKTRACE=1</code>;</li>
59
+
<li>If you want only panics to have backtraces, set <code>RUST_BACKTRACE=1</code> and
60
+
<code>RUST_LIB_BACKTRACE=0</code>.</li>
61
+
</ul>
62
+
</li>
63
+
<li>
64
+
<p>Anyhow works with any error type that has an impl of <code>std::error::Error</code>,
65
+
including ones defined in your crate. We do not bundle a <code>derive(Error)</code>
66
+
macro but you can write the impls yourself or use a standalone macro like
anyhow = { version = "1.0", default-features = false }</code></pre></div>
97
+
<p>With versions of Rust older than 1.81, no_std mode may require an additional
98
+
<code>.map_err(Error::msg)</code> when working with a non-Anyhow error type inside a
99
+
function that returns Anyhow’s error type, as the trait that <code>?</code>-based error
100
+
conversions are defined by is only available in std in those old versions.</p>
101
+
</div></details><h2id="reexports" class="section-header">Re-exports<ahref="#reexports" class="anchor">§</a></h2><dlclass="item-table reexports"><dtid="reexport.format_err"><code>pub use <aclass="macro" href="macro.anyhow.html" title="macro anyhow::anyhow">anyhow</a> as format_err;</code></dt></dl><h2id="macros" class="section-header">Macros<ahref="#macros" class="anchor">§</a></h2><dlclass="item-table"><dt><aclass="macro" href="macro.anyhow.html" title="macro anyhow::anyhow">anyhow</a></dt><dd>Construct an ad-hoc error from a string or existing non-<code>anyhow</code> error
102
+
value.</dd><dt><aclass="macro" href="macro.bail.html" title="macro anyhow::bail">bail</a></dt><dd>Return early with an error.</dd><dt><aclass="macro" href="macro.ensure.html" title="macro anyhow::ensure">ensure</a></dt><dd>Return early with an error if a condition is not satisfied.</dd></dl><h2id="structs" class="section-header">Structs<ahref="#structs" class="anchor">§</a></h2><dlclass="item-table"><dt><aclass="struct" href="struct.Chain.html" title="struct anyhow::Chain">Chain</a></dt><dd>Iterator of a chain of source errors.</dd><dt><aclass="struct" href="struct.Error.html" title="struct anyhow::Error">Error</a></dt><dd>The <code>Error</code> type, a wrapper around a dynamic error type.</dd></dl><h2id="traits" class="section-header">Traits<ahref="#traits" class="anchor">§</a></h2><dlclass="item-table"><dt><aclass="trait" href="trait.Context.html" title="trait anyhow::Context">Context</a></dt><dd>Provides the <code>context</code> method for <code>Result</code>.</dd></dl><h2id="functions" class="section-header">Functions<ahref="#functions" class="anchor">§</a></h2><dlclass="item-table"><dt><aclass="fn" href="fn.Ok.html" title="fn anyhow::Ok">Ok</a></dt><dd>Equivalent to <code>Ok::<_, anyhow::Error>(value)</code>.</dd></dl><h2id="types" class="section-header">Type Aliases<ahref="#types" class="anchor">§</a></h2><dlclass="item-table"><dt><aclass="type" href="type.Result.html" title="type anyhow::Result">Result</a></dt><dd><code>Result<T, Error></code></dd></dl></section></div></main></body></html>
0 commit comments