Sophie

Sophie

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

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

    <title>core::ops::Deref - 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 Deref</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.Target">Target</a></div><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.deref">deref</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: 'Deref', 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=''>Deref</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/deref.rs.html#72-80' title='goto source code'>[src]</a></span></h1>
<pre class='rust trait'><div class="docblock attributes">#[lang = "deref"]
</div>pub trait Deref {
    type <a href='#associatedtype.Target' class="type">Target</a>: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>;
    fn <a href='#tymethod.deref' class='fnname'>deref</a>(&amp;self) -&gt; &amp;Self::<a class="type" href="../../core/ops/trait.Deref.html#associatedtype.Target" title="type core::ops::Deref::Target">Target</a>;
}</pre><div class='docblock'><p>Used for immutable dereferencing operations, like <code>*v</code>.</p>
<p>In addition to being used for explicit dereferencing operations with the
(unary) <code>*</code> operator in immutable contexts, <code>Deref</code> is also used implicitly
by the compiler in many circumstances. This mechanism is called
<a href="#more-on-deref-coercion">'<code>Deref</code> coercion'</a>. In mutable contexts, <a href="trait.DerefMut.html"><code>DerefMut</code></a> is used.</p>
<p>Implementing <code>Deref</code> for smart pointers makes accessing the data behind them
convenient, which is why they implement <code>Deref</code>. On the other hand, the
rules regarding <code>Deref</code> and <a href="trait.DerefMut.html"><code>DerefMut</code></a> were designed specifically to
accommodate smart pointers. Because of this, <strong><code>Deref</code> should only be
implemented for smart pointers</strong> to avoid confusion.</p>
<p>For similar reasons, <strong>this trait should never fail</strong>. Failure during
dereferencing can be extremely confusing when <code>Deref</code> is invoked implicitly.</p>
<h1 id="more-on-deref-coercion" class="section-header"><a href="#more-on-deref-coercion">More on <code>Deref</code> coercion</a></h1>
<p>If <code>T</code> implements <code>Deref&lt;Target = U&gt;</code>, and <code>x</code> is a value of type <code>T</code>, then:</p>
<ul>
<li>In immutable contexts, <code>*x</code> on non-pointer types is equivalent to
<code>*Deref::deref(&amp;x)</code>.</li>
<li>Values of type <code>&amp;T</code> are coerced to values of type <code>&amp;U</code></li>
<li><code>T</code> implicitly implements all the (immutable) methods of the type <code>U</code>.</li>
</ul>
<p>For more details, visit <a href="../../book/second-edition/ch15-02-deref.html">the chapter in <em>The Rust Programming Language</em></a> as well as the reference sections on <a href="../../reference/expressions/operator-expr.html#the-dereference-operator">the dereference operator</a>, <a href="../../reference/expressions/method-call-expr.html">method resolution</a> and <a href="../../reference/type-coercions.html">type coercions</a>.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>A struct with a single field which is accessible by dereferencing the
struct.</p>

<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">ops</span>::<span class="ident">Deref</span>;

<span class="kw">struct</span> <span class="ident">DerefExample</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
    <span class="ident">value</span>: <span class="ident">T</span>
}

<span class="kw">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">Deref</span> <span class="kw">for</span> <span class="ident">DerefExample</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
    <span class="kw">type</span> <span class="ident">Target</span> <span class="op">=</span> <span class="ident">T</span>;

    <span class="kw">fn</span> <span class="ident">deref</span>(<span class="kw-2">&amp;</span><span class="self">self</span>) <span class="op">-&gt;</span> <span class="kw-2">&amp;</span><span class="ident">T</span> {
        <span class="kw-2">&amp;</span><span class="self">self</span>.<span class="ident">value</span>
    }
}

<span class="kw">let</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident">DerefExample</span> { <span class="ident">value</span>: <span class="string">&#39;a&#39;</span> };
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">&#39;a&#39;</span>, <span class="kw-2">*</span><span class="ident">x</span>);<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Afn%20main()%20%7B%0Ause%20std%3A%3Aops%3A%3ADeref%3B%0A%0Astruct%20DerefExample%3CT%3E%20%7B%0A%20%20%20%20value%3A%20T%0A%7D%0A%0Aimpl%3CT%3E%20Deref%20for%20DerefExample%3CT%3E%20%7B%0A%20%20%20%20type%20Target%20%3D%20T%3B%0A%0A%20%20%20%20fn%20deref(%26self)%20-%3E%20%26T%20%7B%0A%20%20%20%20%20%20%20%20%26self.value%0A%20%20%20%20%7D%0A%7D%0A%0Alet%20x%20%3D%20DerefExample%20%7B%20value%3A%20'a'%20%7D%3B%0Aassert_eq!('a'%2C%20*x)%3B%0A%7D">Run</a></pre>
</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.Target' class='method'><span id='Target.t' class='invisible'><code>type <a href='#associatedtype.Target' class="type">Target</a>: ?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></code></span></h3><div class='docblock'><p>The resulting type after dereferencing.</p>
</div></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.deref' class='method'><span id='deref.v' class='invisible'><code>fn <a href='#tymethod.deref' class='fnname'>deref</a>(&amp;self) -&gt; &amp;Self::<a class="type" href="../../core/ops/trait.Deref.html#associatedtype.Target" title="type core::ops::Deref::Target">Target</a></code></span></h3><div class='docblock'><p>Dereferences the value.</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/mem.rs.html#999-1007' title='goto source code'>[src]</a></div><code>impl&lt;T&gt; Deref for <a class="union" href="../../core/mem/union.ManuallyDrop.html" title="union core::mem::ManuallyDrop">ManuallyDrop</a>&lt;T&gt;<span class="where fmt-newline">  type <a href='#associatedtype.Target' class="type">Target</a> = T;</span></code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/deref.rs.html#83-87' title='goto source code'>[src]</a></div><code>impl&lt;'a, T:&nbsp;?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; Deref for &amp;'a T<span class="where fmt-newline">  type <a href='#associatedtype.Target' class="type">Target</a> = T;</span></code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/deref.rs.html#90-94' title='goto source code'>[src]</a></div><code>impl&lt;'a, T:&nbsp;?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; Deref for &amp;'a mut T<span class="where fmt-newline">  type <a href='#associatedtype.Target' class="type">Target</a> = T;</span></code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#989-996' title='goto source code'>[src]</a></div><code>impl&lt;'b, T:&nbsp;?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; Deref for <a class="struct" href="../../core/cell/struct.Ref.html" title="struct core::cell::Ref">Ref</a>&lt;'b, T&gt;<span class="where fmt-newline">  type <a href='#associatedtype.Target' class="type">Target</a> = T;</span></code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#1130-1137' title='goto source code'>[src]</a></div><code>impl&lt;'b, T:&nbsp;?<a class="trait" href="../../core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; Deref for <a class="struct" href="../../core/cell/struct.RefMut.html" title="struct core::cell::RefMut">RefMut</a>&lt;'b, T&gt;<span class="where fmt-newline">  type <a href='#associatedtype.Target' class="type">Target</a> = T;</span></code></li>
</ul><script type="text/javascript" async
                         src="../../implementors/core/ops/trait.Deref.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>