Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > fdedb3cf2140df9b6d419a2338ab1caa > files > 1930

rust-doc-1.25.0-1.mga6.x86_64.rpm

<!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 `Fn` trait in crate `core`.">
    <meta name="keywords" content="rust, rustlang, rust-lang, Fn">

    <title>core::ops::Fn - 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="../../main.css" id="themeStyle">
    <script src="../../storage.js"></script>
    

    <link rel="shortcut icon" href="https://doc.rust-lang.org/favicon.ico">
    
</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">&#9776;</div>
        <a href='../../core/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a>
        <p class='location'>Trait Fn</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.call">call</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>core</a>::<wbr><a href='index.html'>ops</a></p><script>window.sidebarCurrent = {name: 'Fn', 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 js-only">
            <div class="search-container">
                <input class="search-input" name="search"
                       autocomplete="off"
                       placeholder="Click or press ‘S’ to search, ‘?’ for more options…"
                       type="search">
            </div>
        </form>
    </nav>

    <section id='main' class="content">
<h1 class='fqn'><span class='in-band'>Trait <a href='../index.html'>core</a>::<wbr><a href='index.html'>ops</a>::<wbr><a class="trait" href=''>Fn</a></span><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'>&#x2212;</span>]
                   </a>
               </span><a class='srclink' href='../../src/core/ops/function.rs.html#70-74' title='goto source code'>[src]</a></span></h1>
<pre class='rust trait'><div class="docblock attributes">#[lang = "fn"]
</div>pub trait Fn&lt;Args&gt;: <a class="trait" href="../../core/ops/trait.FnMut.html" title="trait core::ops::FnMut">FnMut</a>&lt;Args&gt; {
    extern &quot;rust-call&quot; fn <a href='#tymethod.call' class='fnname'>call</a>(&amp;self, args: Args) -&gt; Self::<a class="type" href="../../core/ops/trait.FnOnce.html#associatedtype.Output" title="type core::ops::FnOnce::Output">Output</a>;
}</pre><div class='docblock'><p>The version of the call operator that takes an immutable receiver.</p>
<p>Instances of <code>Fn</code> can be called repeatedly without mutating state.</p>
<p><em>This trait (<code>Fn</code>) is not to be confused with <a href="../../std/primitive.fn.html">function pointers</a>
(<code>fn</code>).</em></p>
<p><code>Fn</code> is implemented automatically by closures which only take immutable
references to captured variables or don't capture anything at all, as well
as (safe) <a href="../../std/primitive.fn.html">function pointers</a> (with some caveats, see their documentation
for more details). Additionally, for any type <code>F</code> that implements <code>Fn</code>, <code>&amp;F</code>
implements <code>Fn</code>, too.</p>
<p>Since both <a href="trait.FnMut.html"><code>FnMut</code></a> and <a href="trait.FnOnce.html"><code>FnOnce</code></a> are supertraits of <code>Fn</code>, any
instance of <code>Fn</code> can be used as a parameter where a <a href="trait.FnMut.html"><code>FnMut</code></a> or <a href="trait.FnOnce.html"><code>FnOnce</code></a>
is expected.</p>
<p>Use <code>Fn</code> as a bound when you want to accept a parameter of function-like
type and need to call it repeatedly and without mutating state (e.g. when
calling it concurrently). If you do not need such strict requirements, use
<a href="trait.FnMut.html"><code>FnMut</code></a> or <a href="trait.FnOnce.html"><code>FnOnce</code></a> as bounds.</p>
<p>See the <a href="../../book/second-edition/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) -&gt; 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="calling-a-closure" class="section-header"><a href="#calling-a-closure">Calling a closure</a></h2>
<pre class="rust rust-example-rendered">
<span class="kw">let</span> <span class="ident">square</span> <span class="op">=</span> <span class="op">|</span><span class="ident">x</span><span class="op">|</span> <span class="ident">x</span> <span class="op">*</span> <span class="ident">x</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">square</span>(<span class="number">5</span>), <span class="number">25</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Alet%20square%20%3D%20%7Cx%7C%20x%20*%20x%3B%0Aassert_eq!(square(5)%2C%2025)%3B%0A%7D">Run</a></pre>
<h2 id="using-a-fn-parameter" class="section-header"><a href="#using-a-fn-parameter">Using a <code>Fn</code> parameter</a></h2>
<pre class="rust rust-example-rendered">
<span class="kw">fn</span> <span class="ident">call_with_one</span><span class="op">&lt;</span><span class="ident">F</span><span class="op">&gt;</span>(<span class="ident">func</span>: <span class="ident">F</span>) <span class="op">-&gt;</span> <span class="ident">usize</span>
    <span class="kw">where</span> <span class="ident">F</span>: <span class="ident">Fn</span>(<span class="ident">usize</span>) <span class="op">-&gt;</span> <span class="ident">usize</span> {
    <span class="ident">func</span>(<span class="number">1</span>)
}

<span class="kw">let</span> <span class="ident">double</span> <span class="op">=</span> <span class="op">|</span><span class="ident">x</span><span class="op">|</span> <span class="ident">x</span> <span class="op">*</span> <span class="number">2</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">call_with_one</span>(<span class="ident">double</span>), <span class="number">2</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Afn%20call_with_one%3CF%3E(func%3A%20F)%20-%3E%20usize%0A%20%20%20%20where%20F%3A%20Fn(usize)%20-%3E%20usize%20%7B%0A%20%20%20%20func(1)%0A%7D%0A%0Alet%20double%20%3D%20%7Cx%7C%20x%20*%202%3B%0Aassert_eq!(call_with_one(double)%2C%202)%3B%0A%7D">Run</a></pre>
</div>
            <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' class='method'><span id='call.v' class='invisible'><code>extern &quot;rust-call&quot; fn <a href='#tymethod.call' class='fnname'>call</a>(&amp;self, args: Args) -&gt; Self::<a class="type" href="../../core/ops/trait.FnOnce.html#associatedtype.Output" title="type core::ops::FnOnce::Output">Output</a></code></span></h3><div class='stability'><div class='stab unstable'><span class=microscope>🔬</span> This is a nightly-only experimental API.  (<code>fn_traits </code><a href="https://github.com/rust-lang/rust/issues/29625">#29625</a>)</div></div><div class='docblock'><p>Performs the call operation.</p>
</div></div>
        <h2 id='implementors' class='small-section-header'>
          Implementors<a href='#implementors' class='anchor'></a>
        </h2>
        <ul class='item-list' id='implementors-list'>
    <li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/function.rs.html#228-234' title='goto source code'>[src]</a></div><code>impl&lt;'a, A, F:&nbsp;?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; Fn&lt;A&gt; for &amp;'a F <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="../../core/ops/trait.Fn.html" title="trait core::ops::Fn">Fn</a>&lt;A&gt;,&nbsp;</span></code></li>
</ul><script type="text/javascript" async
                         src="../../implementors/core/ops/trait.Fn.js">
                 </script></section>
    <section id='search' class="content hidden"></section>

    <section class="footer"></section>

    <aside id="help" class="hidden">
        <div>
            <h1 class="hidden">Help</h1>

            <div class="shortcuts">
                <h2>Keyboard Shortcuts</h2>

                <dl>
                    <dt><kbd>?</kbd></dt>
                    <dd>Show this help dialog</dd>
                    <dt><kbd>S</kbd></dt>
                    <dd>Focus the search field</dd>
                    <dt><kbd>↑</kbd></dt>
                    <dd>Move up in search results</dd>
                    <dt><kbd>↓</kbd></dt>
                    <dd>Move down in search results</dd>
                    <dt><kbd>↹</kbd></dt>
                    <dd>Switch tab</dd>
                    <dt><kbd>&#9166;</kbd></dt>
                    <dd>Go to active search result</dd>
                    <dt><kbd>+</kbd></dt>
                    <dd>Expand all sections</dd>
                    <dt><kbd>-</kbd></dt>
                    <dd>Collapse all sections</dd>
                </dl>
            </div>

            <div class="infos">
                <h2>Search Tricks</h2>

                <p>
                    Prefix searches with a type followed by a colon (e.g.
                    <code>fn:</code>) to restrict the search to a given type.
                </p>

                <p>
                    Accepted types are: <code>fn</code>, <code>mod</code>,
                    <code>struct</code>, <code>enum</code>,
                    <code>trait</code>, <code>type</code>, <code>macro</code>,
                    and <code>const</code>.
                </p>

                <p>
                    Search functions by type signature (e.g.
                    <code>vec -> usize</code> or <code>* -> vec</code>)
                </p>
            </div>
        </div>
    </aside>

    

    <script>
        window.rootPath = "../../";
        window.currentCrate = "core";
    </script>
    <script src="../../main.js"></script>
    <script defer src="../../search-index.js"></script>
</body>
</html>