Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates > by-pkgid > 4e2dbb669434a7691662cb2f0ad38972 > files > 10018

rust-doc-1.28.0-1.mga6.armv7hl.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 `Drop` trait in crate `core`."><meta name="keywords" content="rust, rustlang, rust-lang, Drop"><title>core::ops::Drop - 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><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 Drop</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.drop">drop</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: 'Drop', 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"><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='in-band'>Trait <a href='../index.html'>core</a>::<wbr><a href='index.html'>ops</a>::<wbr><a class="trait" href=''>Drop</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/drop.rs.html#95-117' title='goto source code'>[src]</a></span></h1><div class="docblock type-decl"><pre class='rust trait'><div class="docblock attributes">#[lang = "drop"]
</div>pub trait Drop {
    fn <a href='#tymethod.drop' class='fnname'>drop</a>(&amp;mut self);
}</pre></div><div class='docblock'><p>Used to run some code when a value goes out of scope.
This is sometimes called a 'destructor'.</p>
<p>When a value goes out of scope, it will have its <code>drop</code> method called if
its type implements <code>Drop</code>. Then, any fields the value contains will also
be dropped recursively.</p>
<p>Because of this recursive dropping, you do not need to implement this trait
unless your type needs its own destructor logic.</p>
<p>Refer to <a href="../../book/second-edition/ch15-03-drop.html">the chapter on <code>Drop</code> in <em>The Rust Programming Language</em></a>
for some more elaboration.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1><h2 id="implementing-drop" class="section-header"><a href="#implementing-drop">Implementing <code>Drop</code></a></h2>
<p>The <code>drop</code> method is called when <code>_x</code> goes out of scope, and therefore
<code>main</code> prints <code>Dropping!</code>.</p>

<pre class="rust rust-example-rendered">
<span class="kw">struct</span> <span class="ident">HasDrop</span>;

<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">HasDrop</span> {
    <span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) {
        <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Dropping!&quot;</span>);
    }
}

<span class="kw">fn</span> <span class="ident">main</span>() {
    <span class="kw">let</span> <span class="ident">_x</span> <span class="op">=</span> <span class="ident">HasDrop</span>;
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Astruct%20HasDrop%3B%0A%0Aimpl%20Drop%20for%20HasDrop%20%7B%0A%20%20%20%20fn%20drop(%26mut%20self)%20%7B%0A%20%20%20%20%20%20%20%20println!(%22Dropping!%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20_x%20%3D%20HasDrop%3B%0A%7D">Run</a></pre>
<h2 id="dropping-is-done-recursively" class="section-header"><a href="#dropping-is-done-recursively">Dropping is done recursively</a></h2>
<p>When <code>outer</code> goes out of scope, the <code>drop</code> method will be called first for
<code>Outer</code>, then for <code>Inner</code>. Therefore, <code>main</code> prints <code>Dropping Outer!</code> and
then <code>Dropping Inner!</code>.</p>

<pre class="rust rust-example-rendered">
<span class="kw">struct</span> <span class="ident">Inner</span>;
<span class="kw">struct</span> <span class="ident">Outer</span>(<span class="ident">Inner</span>);

<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">Inner</span> {
    <span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) {
        <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Dropping Inner!&quot;</span>);
    }
}

<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">Outer</span> {
    <span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) {
        <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;Dropping Outer!&quot;</span>);
    }
}

<span class="kw">fn</span> <span class="ident">main</span>() {
    <span class="kw">let</span> <span class="ident">_x</span> <span class="op">=</span> <span class="ident">Outer</span>(<span class="ident">Inner</span>);
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Astruct%20Inner%3B%0Astruct%20Outer(Inner)%3B%0A%0Aimpl%20Drop%20for%20Inner%20%7B%0A%20%20%20%20fn%20drop(%26mut%20self)%20%7B%0A%20%20%20%20%20%20%20%20println!(%22Dropping%20Inner!%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%0Aimpl%20Drop%20for%20Outer%20%7B%0A%20%20%20%20fn%20drop(%26mut%20self)%20%7B%0A%20%20%20%20%20%20%20%20println!(%22Dropping%20Outer!%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20_x%20%3D%20Outer(Inner)%3B%0A%7D">Run</a></pre>
<h2 id="variables-are-dropped-in-reverse-order-of-declaration" class="section-header"><a href="#variables-are-dropped-in-reverse-order-of-declaration">Variables are dropped in reverse order of declaration</a></h2>
<p><code>_first</code> is declared first and <code>_second</code> is declared second, so <code>main</code> will
print <code>Declared second!</code> and then <code>Declared first!</code>.</p>

<pre class="rust rust-example-rendered">
<span class="kw">struct</span> <span class="ident">PrintOnDrop</span>(<span class="kw-2">&amp;</span><span class="lifetime">&#39;static</span> <span class="ident">str</span>);

<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">PrintOnDrop</span> {
    <span class="kw">fn</span> <span class="ident">drop</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="self">self</span>) {
        <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="self">self</span>.<span class="number">0</span>);
    }
}

<span class="kw">fn</span> <span class="ident">main</span>() {
    <span class="kw">let</span> <span class="ident">_first</span> <span class="op">=</span> <span class="ident">PrintOnDrop</span>(<span class="string">&quot;Declared first!&quot;</span>);
    <span class="kw">let</span> <span class="ident">_second</span> <span class="op">=</span> <span class="ident">PrintOnDrop</span>(<span class="string">&quot;Declared second!&quot;</span>);
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Astruct%20PrintOnDrop(%26'static%20str)%3B%0A%0Aimpl%20Drop%20for%20PrintOnDrop%20%7B%0A%20%20%20%20fn%20drop(%26mut%20self)%20%7B%0A%20%20%20%20%20%20%20%20println!(%22%7B%7D%22%2C%20self.0)%3B%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20_first%20%3D%20PrintOnDrop(%22Declared%20first!%22)%3B%0A%20%20%20%20let%20_second%20%3D%20PrintOnDrop(%22Declared%20second!%22)%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.drop' class='method'><span id='drop.v' class='invisible'><code>fn <a href='#tymethod.drop' class='fnname'>drop</a>(&amp;mut self)</code></span></h3><div class='docblock'><p>Executes the destructor for this type.</p>
<p>This method is called implicitly when the value goes out of scope,
and cannot be called explicitly (this is compiler error <a href="../../error-index.html#E0040">E0040</a>).
However, the <a href="../../std/mem/fn.drop.html"><code>std::mem::drop</code></a> function in the prelude can be
used to call the argument's <code>Drop</code> implementation.</p>
<p>When this method has been called, <code>self</code> has not yet been deallocated.
That only happens after the method is over.
If this wasn't the case, <code>self</code> would be a dangling reference.</p>
<h1 id="panics" class="section-header"><a href="#panics">Panics</a></h1>
<p>Given that a <a href="../macro.panic.html"><code>panic!</code></a> will call <code>drop</code> as it unwinds, any <a href="../macro.panic.html"><code>panic!</code></a>
in a <code>drop</code> implementation will likely abort.</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><table class='table-display'><tbody><tr><td><code>impl Drop for <a class="struct" href="../../core/task/struct.Waker.html" title="struct core::task::Waker">Waker</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/core/task.rs.html#156-163' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Drop for <a class="struct" href="../../core/task/struct.LocalWaker.html" title="struct core::task::LocalWaker">LocalWaker</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/core/task.rs.html#253-260' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
<li><table class='table-display'><tbody><tr><td><code>impl Drop for <a class="struct" href="../../core/task/struct.TaskObj.html" title="struct core::task::TaskObj">TaskObj</a></code><td><div class='out-of-band'><a class='srclink' href='../../src/core/task.rs.html#528-534' title='goto source code'>[src]</a></div></td></tr></tbody></table></li>
</ul><script type="text/javascript">window.inlined_types=new Set([]);</script><script type="text/javascript" async
                         src="../../implementors/core/ops/trait.Drop.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><p>Search multiple things at once by splitting your query with comma (e.g. <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../../";window.currentCrate = "core";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>