Sophie

Sophie

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

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

    <title>core::ops::DerefMut - 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 DerefMut</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.deref_mut">deref_mut</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: 'DerefMut', 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=''>DerefMut</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#166-170' title='goto source code'>[src]</a></span></h1>
<pre class='rust trait'><div class="docblock attributes">#[lang = "deref_mut"]
</div>pub trait DerefMut: <a class="trait" href="../../core/ops/trait.Deref.html" title="trait core::ops::Deref">Deref</a> {
    fn <a href='#tymethod.deref_mut' class='fnname'>deref_mut</a>(&amp;mut self) -&gt; &amp;mut 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 mutable dereferencing operations, like in <code>*v = 1;</code>.</p>
<p>In addition to being used for explicit dereferencing operations with the
(unary) <code>*</code> operator in mutable contexts, <code>DerefMut</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 immutable contexts, <a href="trait.Deref.html"><code>Deref</code></a> is used.</p>
<p>Implementing <code>DerefMut</code> for smart pointers makes mutating the data behind
them convenient, which is why they implement <code>DerefMut</code>. On the other hand,
the rules regarding <a href="trait.Deref.html"><code>Deref</code></a> and <code>DerefMut</code> were designed specifically to
accommodate smart pointers. Because of this, <strong><code>DerefMut</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>DerefMut</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>DerefMut&lt;Target = U&gt;</code>, and <code>x</code> is a value of type <code>T</code>,
then:</p>
<ul>
<li>In mutable contexts, <code>*x</code> on non-pointer types is equivalent to
<code>*Deref::deref(&amp;x)</code>.</li>
<li>Values of type <code>&amp;mut T</code> are coerced to values of type <code>&amp;mut U</code></li>
<li><code>T</code> implicitly implements all the (mutable) 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 modifiable 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="ident">DerefMut</span>};

<span class="kw">struct</span> <span class="ident">DerefMutExample</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">DerefMutExample</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">impl</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> <span class="ident">DerefMut</span> <span class="kw">for</span> <span class="ident">DerefMutExample</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span> {
    <span class="kw">fn</span> <span class="ident">deref_mut</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) <span class="op">-&gt;</span> <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">T</span> {
        <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>.<span class="ident">value</span>
    }
}

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">x</span> <span class="op">=</span> <span class="ident">DerefMutExample</span> { <span class="ident">value</span>: <span class="string">&#39;a&#39;</span> };
<span class="kw-2">*</span><span class="ident">x</span> <span class="op">=</span> <span class="string">&#39;b&#39;</span>;
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="string">&#39;b&#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%3A%7BDeref%2C%20DerefMut%7D%3B%0A%0Astruct%20DerefMutExample%3CT%3E%20%7B%0A%20%20%20%20value%3A%20T%0A%7D%0A%0Aimpl%3CT%3E%20Deref%20for%20DerefMutExample%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%0Aimpl%3CT%3E%20DerefMut%20for%20DerefMutExample%3CT%3E%20%7B%0A%20%20%20%20fn%20deref_mut(%26mut%20self)%20-%3E%20%26mut%20T%20%7B%0A%20%20%20%20%20%20%20%20%26mut%20self.value%0A%20%20%20%20%7D%0A%7D%0A%0Alet%20mut%20x%20%3D%20DerefMutExample%20%7B%20value%3A%20'a'%20%7D%3B%0A*x%20%3D%20'b'%3B%0Aassert_eq!('b'%2C%20*x)%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.deref_mut' class='method'><span id='deref_mut.v' class='invisible'><code>fn <a href='#tymethod.deref_mut' class='fnname'>deref_mut</a>(&amp;mut self) -&gt; &amp;mut 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>Mutably 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#1010-1017' title='goto source code'>[src]</a></div><code>impl&lt;T&gt; DerefMut for <a class="union" href="../../core/mem/union.ManuallyDrop.html" title="union core::mem::ManuallyDrop">ManuallyDrop</a>&lt;T&gt;</code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/core/ops/deref.rs.html#173-175' 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; DerefMut for &amp;'a mut T</code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/core/cell.rs.html#1140-1145' 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; DerefMut for <a class="struct" href="../../core/cell/struct.RefMut.html" title="struct core::cell::RefMut">RefMut</a>&lt;'b, T&gt;</code></li>
</ul><script type="text/javascript" async
                         src="../../implementors/core/ops/trait.DerefMut.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>