45 lines
6.9 KiB
HTML
45 lines
6.9 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 `identity` fn in crate `nom`."><meta name="keywords" content="rust, rustlang, rust-lang, identity"><title>nom::lib::std::convert::identity - 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 fn"><!--[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><div class="sidebar-elems"><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'>convert</a></p><script>window.sidebarCurrent = {name: 'identity', ty: 'fn', 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.33.0'>1.33.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/convert/mod.rs.html#106-108' title='goto source code'>[src]</a></span><span class='in-band'>Function <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'>convert</a>::<wbr><a class="fn" href=''>identity</a></span></h1><pre class='rust fn'>pub const fn identity<T>(x: T) -> T</pre><div class='docblock'><p>The identity function.</p>
|
||
<p>Two things are important to note about this function:</p>
|
||
<ul>
|
||
<li>
|
||
<p>It is not always equivalent to a closure like <code>|x| x</code>, since the
|
||
closure may coerce <code>x</code> into a different type.</p>
|
||
</li>
|
||
<li>
|
||
<p>It moves the input <code>x</code> passed to the function.</p>
|
||
</li>
|
||
</ul>
|
||
<p>While it might seem strange to have a function that just returns back the
|
||
input, there are some interesting uses.</p>
|
||
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
|
||
<p>Using <code>identity</code> to do nothing in a sequence of other, interesting,
|
||
functions:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">convert</span>::<span class="ident">identity</span>;
|
||
|
||
<span class="kw">fn</span> <span class="ident">manipulation</span>(<span class="ident">x</span>: <span class="ident">u32</span>) <span class="op">-</span><span class="op">></span> <span class="ident">u32</span> {
|
||
<span class="comment">// Let's pretend that adding one is an interesting function.</span>
|
||
<span class="ident">x</span> <span class="op">+</span> <span class="number">1</span>
|
||
}
|
||
|
||
<span class="kw">let</span> <span class="ident">_arr</span> <span class="op">=</span> <span class="kw-2">&</span>[<span class="ident">identity</span>, <span class="ident">manipulation</span>];</pre></div>
|
||
<p>Using <code>identity</code> as a "do nothing" base case in a conditional:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">convert</span>::<span class="ident">identity</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">do_stuff</span> <span class="op">=</span> <span class="kw">if</span> <span class="ident">condition</span> { <span class="ident">manipulation</span> } <span class="kw">else</span> { <span class="ident">identity</span> };
|
||
|
||
<span class="comment">// Do more interesting stuff...</span>
|
||
|
||
<span class="kw">let</span> <span class="ident">_results</span> <span class="op">=</span> <span class="ident">do_stuff</span>(<span class="number">42</span>);</pre></div>
|
||
<p>Using <code>identity</code> to keep the <code>Some</code> variants of an iterator of <code>Option<T></code>:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">convert</span>::<span class="ident">identity</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">iter</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="prelude-val">Some</span>(<span class="number">1</span>), <span class="prelude-val">None</span>, <span class="prelude-val">Some</span>(<span class="number">3</span>)].<span class="ident">into_iter</span>();
|
||
<span class="kw">let</span> <span class="ident">filtered</span> <span class="op">=</span> <span class="ident">iter</span>.<span class="ident">filter_map</span>(<span class="ident">identity</span>).<span class="ident">collect</span>::<span class="op"><</span><span class="ident">Vec</span><span class="op"><</span><span class="kw">_</span><span class="op">></span><span class="op">></span>();
|
||
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="macro">vec</span><span class="macro">!</span>[<span class="number">1</span>, <span class="number">3</span>], <span class="ident">filtered</span>);</pre></div>
|
||
</div></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> |