50 lines
18 KiB
HTML
50 lines
18 KiB
HTML
<!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="API documentation for the Rust `FnOnce` trait in crate `nom`."><meta name="keywords" content="rust, rustlang, rust-lang, FnOnce"><title>nom::lib::std::ops::FnOnce - Rust</title><link rel="stylesheet" type="text/css" href="../../../../normalize.css"><link rel="stylesheet" type="text/css" href="../../../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../../../dark.css"><link rel="stylesheet" type="text/css" href="../../../../light.css" id="themeStyle"><script src="../../../../storage.js"></script><noscript><link rel="stylesheet" href="../../../../noscript.css"></noscript><link rel="shortcut icon" href="../../../../favicon.ico"><style type="text/css">#crate-search{background-image:url("../../../../down-arrow.svg");}</style></head><body class="rustdoc trait"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../../../../nom/index.html'><div class='logo-container'><img src='../../../../rust-logo.png' alt='logo'></div></a><p class='location'>Trait FnOnce</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#associated-types">Associated Types</a><div class="sidebar-links"><a href="#associatedtype.Output">Output</a></div><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.call_once">call_once</a></div><a class="sidebar-title" href="#foreign-impls">Implementations on Foreign Types</a><div class="sidebar-links"><a href="#impl-FnOnce%3CA%3E-for-%26%27_%20F">&'_ F</a><a href="#impl-FnOnce%3CA%3E-for-%26%27_%20mut%20F">&'_ mut F</a><a href="#impl-FnOnce()-for-AssertUnwindSafe%3CF%3E">AssertUnwindSafe<F></a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../../../index.html'>nom</a>::<wbr><a href='../../index.html'>lib</a>::<wbr><a href='../index.html'>std</a>::<wbr><a href='index.html'>ops</a></p><script>window.sidebarCurrent = {name: 'FnOnce', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../../../../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../../../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../../../settings.html"><img src="../../../../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span class='since' title='Stable since Rust version 1.0.0'>1.0.0</span><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/ops/function.rs.html#225-233' title='goto source code'>[src]</a></span><span class='in-band'>Trait <a href='../../../index.html'>nom</a>::<wbr><a href='../../index.html'>lib</a>::<wbr><a href='../index.html'>std</a>::<wbr><a href='index.html'>ops</a>::<wbr><a class="trait" href=''>FnOnce</a></span></h1><div class="docblock type-decl hidden-by-usual-hider"><pre class='rust trait'><span class="docblock attributes top-attr">#[lang = "fn_once"]
|
||
#[must_use = "closures are lazy and do nothing unless called"]
|
||
</span>pub trait FnOnce<Args> {
|
||
type <a href='#associatedtype.Output' class="type">Output</a>;
|
||
extern "rust-call" fn <a href='#tymethod.call_once' class='fnname'>call_once</a>(self, args: Args) -> Self::<a class="type" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html#associatedtype.Output" title="type nom::lib::std::prelude::v1::v1::FnOnce::Output">Output</a>;
|
||
}</pre></div><div class='docblock'><p>The version of the call operator that takes a by-value receiver.</p>
|
||
<p>Instances of <code>FnOnce</code> can be called, but might not be callable multiple
|
||
times. Because of this, if the only thing known about a type is that it
|
||
implements <code>FnOnce</code>, it can only be called once.</p>
|
||
<p><code>FnOnce</code> is implemented automatically by closure that might consume captured
|
||
variables, as well as all types that implement <a href="trait.FnMut.html"><code>FnMut</code></a>, e.g., (safe)
|
||
<a href="../../std/primitive.fn.html">function pointers</a> (since <code>FnOnce</code> is a supertrait of <a href="trait.FnMut.html"><code>FnMut</code></a>).</p>
|
||
<p>Since both <a href="trait.Fn.html"><code>Fn</code></a> and <a href="trait.FnMut.html"><code>FnMut</code></a> are subtraits of <code>FnOnce</code>, any instance of
|
||
<a href="trait.Fn.html"><code>Fn</code></a> or <a href="trait.FnMut.html"><code>FnMut</code></a> can be used where a <code>FnOnce</code> is expected.</p>
|
||
<p>Use <code>FnOnce</code> as a bound when you want to accept a parameter of function-like
|
||
type and only need to call it once. If you need to call the parameter
|
||
repeatedly, use <a href="trait.FnMut.html"><code>FnMut</code></a> as a bound; if you also need it to not mutate
|
||
state, use <a href="trait.Fn.html"><code>Fn</code></a>.</p>
|
||
<p>See the <a href="../../book/ch13-01-closures.html">chapter on closures in <em>The Rust Programming Language</em></a> for
|
||
some more information on this topic.</p>
|
||
<p>Also of note is the special syntax for <code>Fn</code> traits (e.g.
|
||
<code>Fn(usize, bool) -> usize</code>). Those interested in the technical details of
|
||
this can refer to <a href="../../nomicon/hrtb.html">the relevant section in the <em>Rustonomicon</em></a>.</p>
|
||
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1><h2 id="using-a-fnonce-parameter" class="section-header"><a href="#using-a-fnonce-parameter">Using a <code>FnOnce</code> parameter</a></h2>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">fn</span> <span class="ident">consume_with_relish</span><span class="op"><</span><span class="ident">F</span><span class="op">></span>(<span class="ident">func</span>: <span class="ident">F</span>)
|
||
<span class="kw">where</span> <span class="ident">F</span>: <span class="ident">FnOnce</span>() <span class="op">-</span><span class="op">></span> <span class="ident">String</span>
|
||
{
|
||
<span class="comment">// `func` consumes its captured variables, so it cannot be run more</span>
|
||
<span class="comment">// than once.</span>
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Consumed: {}"</span>, <span class="ident">func</span>());
|
||
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Delicious!"</span>);
|
||
|
||
<span class="comment">// Attempting to invoke `func()` again will throw a `use of moved</span>
|
||
<span class="comment">// value` error for `func`.</span>
|
||
}
|
||
|
||
<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">"x"</span>);
|
||
<span class="kw">let</span> <span class="ident">consume_and_return_x</span> <span class="op">=</span> <span class="kw">move</span> <span class="op">|</span><span class="op">|</span> <span class="ident">x</span>;
|
||
<span class="ident">consume_with_relish</span>(<span class="ident">consume_and_return_x</span>);
|
||
|
||
<span class="comment">// `consume_and_return_x` can no longer be invoked at this point</span></pre></div>
|
||
</div>
|
||
<h2 id='associated-types' class='small-section-header'>Associated Types<a href='#associated-types' class='anchor'></a></h2><div class='methods'><h3 id='associatedtype.Output' class='method'><code id='Output.t'>type <a href='#associatedtype.Output' class="type">Output</a></code><span class='since' title='Stable since Rust version 1.12.0'>1.12.0</span></h3><div class='docblock'><p>The returned type after the call operator is used.</p>
|
||
</div></div><span class='loading-content'>Loading content...</span>
|
||
<h2 id='required-methods' class='small-section-header'>Required methods<a href='#required-methods' class='anchor'></a></h2><div class='methods'><h3 id='tymethod.call_once' class='method'><code id='call_once.v'>extern "rust-call" fn <a href='#tymethod.call_once' class='fnname'>call_once</a>(self, args: Args) -> Self::<a class="type" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html#associatedtype.Output" title="type nom::lib::std::prelude::v1::v1::FnOnce::Output">Output</a></code></h3><div class='stability'><div class='stab unstable'><span class='emoji'>🔬</span> This is a nightly-only experimental API. (<code>fn_traits</code>)</div></div><div class='docblock'><p>Performs the call operation.</p>
|
||
</div></div><span class='loading-content'>Loading content...</span>
|
||
<h2 id='foreign-impls' class='small-section-header'>Implementations on Foreign Types<a href='#foreign-impls' class='anchor'></a></h2><h3 id='impl-FnOnce()-for-AssertUnwindSafe%3CF%3E' class='impl'><code class='in-band'>impl<R, F> <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html" title="trait nom::lib::std::prelude::v1::v1::FnOnce">FnOnce</a>() for <a class="struct" href="https://doc.rust-lang.org/nightly/std/panic/struct.AssertUnwindSafe.html" title="struct std::panic::AssertUnwindSafe">AssertUnwindSafe</a><F> <span class="where fmt-newline">where<br> F: <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html" title="trait nom::lib::std::prelude::v1::v1::FnOnce">FnOnce</a>() -> R, </span></code><a href='#impl-FnOnce()-for-AssertUnwindSafe%3CF%3E' class='anchor'></a><a class='srclink' href='https://doc.rust-lang.org/nightly/src/std/panic.rs.html#314-320' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='associatedtype.Output-1' class="type"><code id='Output.t-1'>type <a href='#associatedtype.Output' class="type">Output</a> = R</code></h4><h4 id='method.call_once' class="method hidden"><code id='call_once.v-1'>extern "rust-call" fn <a href='#method.call_once' class='fnname'>call_once</a>(self, _args: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.unit.html">()</a>) -> R</code><a class='srclink' href='https://doc.rust-lang.org/nightly/src/std/panic.rs.html#317-319' title='goto source code'>[src]</a></h4><div class='stability hidden'><div class='stab unstable'><span class='emoji'>🔬</span> This is a nightly-only experimental API. (<code>fn_traits</code>)</div></div></div><h3 id='impl-FnOnce%3CA%3E-for-%26%27_%20F' class='impl'><code class='in-band'>impl<'_, A, F> <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html" title="trait nom::lib::std::prelude::v1::v1::FnOnce">FnOnce</a><A> for <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&'_ </a>F <span class="where fmt-newline">where<br> F: <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.Fn.html" title="trait nom::lib::std::prelude::v1::v1::Fn">Fn</a><A> + ?<a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.Sized.html" title="trait nom::lib::std::prelude::v1::v1::Sized">Sized</a>, </span></code><a href='#impl-FnOnce%3CA%3E-for-%26%27_%20F' class='anchor'></a><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/ops/function.rs.html#257-266' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='associatedtype.Output-2' class="type"><code id='Output.t-2'>type <a href='#associatedtype.Output' class="type">Output</a> = <F as <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html" title="trait nom::lib::std::prelude::v1::v1::FnOnce">FnOnce</a><A>>::<a class="type" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html#associatedtype.Output" title="type nom::lib::std::prelude::v1::v1::FnOnce::Output">Output</a></code></h4><h4 id='method.call_once-1' class="method hidden"><code id='call_once.v-2'>extern "rust-call" fn <a href='#method.call_once' class='fnname'>call_once</a>(self, args: A) -> <F as <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html" title="trait nom::lib::std::prelude::v1::v1::FnOnce">FnOnce</a><A>>::<a class="type" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html#associatedtype.Output" title="type nom::lib::std::prelude::v1::v1::FnOnce::Output">Output</a></code><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/ops/function.rs.html#263-265' title='goto source code'>[src]</a></h4><div class='stability hidden'><div class='stab unstable'><span class='emoji'>🔬</span> This is a nightly-only experimental API. (<code>fn_traits</code>)</div></div></div><h3 id='impl-FnOnce%3CA%3E-for-%26%27_%20mut%20F' class='impl'><code class='in-band'>impl<'_, A, F> <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html" title="trait nom::lib::std::prelude::v1::v1::FnOnce">FnOnce</a><A> for <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&'_ mut </a>F <span class="where fmt-newline">where<br> F: <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnMut.html" title="trait nom::lib::std::prelude::v1::v1::FnMut">FnMut</a><A> + ?<a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.Sized.html" title="trait nom::lib::std::prelude::v1::v1::Sized">Sized</a>, </span></code><a href='#impl-FnOnce%3CA%3E-for-%26%27_%20mut%20F' class='anchor'></a><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/ops/function.rs.html#279-287' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='associatedtype.Output-3' class="type"><code id='Output.t-3'>type <a href='#associatedtype.Output' class="type">Output</a> = <F as <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html" title="trait nom::lib::std::prelude::v1::v1::FnOnce">FnOnce</a><A>>::<a class="type" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html#associatedtype.Output" title="type nom::lib::std::prelude::v1::v1::FnOnce::Output">Output</a></code></h4><h4 id='method.call_once-2' class="method hidden"><code id='call_once.v-3'>extern "rust-call" fn <a href='#method.call_once' class='fnname'>call_once</a>(self, args: A) -> <F as <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html" title="trait nom::lib::std::prelude::v1::v1::FnOnce">FnOnce</a><A>>::<a class="type" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html#associatedtype.Output" title="type nom::lib::std::prelude::v1::v1::FnOnce::Output">Output</a></code><a class='srclink' href='https://doc.rust-lang.org/nightly/src/core/ops/function.rs.html#284-286' title='goto source code'>[src]</a></h4><div class='stability hidden'><div class='stab unstable'><span class='emoji'>🔬</span> This is a nightly-only experimental API. (<code>fn_traits</code>)</div></div></div><span class='loading-content'>Loading content...</span>
|
||
<h2 id='implementors' class='small-section-header'>Implementors<a href='#implementors' class='anchor'></a></h2><div class='item-list' id='implementors-list'><h3 id='impl-FnOnce%3CA%3E' class='impl'><code class='in-band'>impl<A, F> FnOnce<A> for <a class="struct" href="../../../../nom/lib/std/prelude/v1/v1/struct.Box.html" title="struct nom::lib::std::prelude::v1::v1::Box">Box</a><F> <span class="where fmt-newline">where<br> F: <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html" title="trait nom::lib::std::prelude::v1::v1::FnOnce">FnOnce</a><A> + ?<a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.Sized.html" title="trait nom::lib::std::prelude::v1::v1::Sized">Sized</a>, </span></code><a href='#impl-FnOnce%3CA%3E' class='anchor'></a><a class='srclink' href='https://doc.rust-lang.org/nightly/src/alloc/boxed.rs.html#1013-1019' title='goto source code'>[src]</a></h3><div class='impl-items'><h4 id='associatedtype.Output-4' class="type"><code id='Output.t-4'>type <a href='#associatedtype.Output-4' class="type">Output</a> = <F as <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html" title="trait nom::lib::std::prelude::v1::v1::FnOnce">FnOnce</a><A>>::<a class="type" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html#associatedtype.Output" title="type nom::lib::std::prelude::v1::v1::FnOnce::Output">Output</a></code></h4><h4 id='method.call_once-3' class="method hidden"><code id='call_once.v-4'>extern "rust-call" fn <a href='#method.call_once-3' class='fnname'>call_once</a>(self, args: A) -> <<a class="struct" href="../../../../nom/lib/std/prelude/v1/v1/struct.Box.html" title="struct nom::lib::std::prelude::v1::v1::Box">Box</a><F> as <a class="trait" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html" title="trait nom::lib::std::prelude::v1::v1::FnOnce">FnOnce</a><A>>::<a class="type" href="../../../../nom/lib/std/prelude/v1/v1/trait.FnOnce.html#associatedtype.Output" title="type nom::lib::std::prelude::v1::v1::FnOnce::Output">Output</a></code><a class='srclink' href='https://doc.rust-lang.org/nightly/src/alloc/boxed.rs.html#1016-1018' title='goto source code'>[src]</a></h4><div class='stability hidden'><div class='stab unstable'><span class='emoji'>🔬</span> This is a nightly-only experimental API. (<code>fn_traits</code>)</div></div></div></div><span class='loading-content'>Loading content...</span><script type="text/javascript" src="../../../../implementors/core/ops/function/trait.FnOnce.js" async></script></section><section id="search" class="content hidden"></section><section class="footer"></section><script>window.rootPath = "../../../../";window.currentCrate = "nom";</script><script src="../../../../aliases.js"></script><script src="../../../../main.js"></script><script defer src="../../../../search-index.js"></script></body></html> |