<!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 `std`."> <meta name="keywords" content="rust, rustlang, rust-lang, Drop"> <title>std::ops::Drop - Rust</title> <link rel="stylesheet" type="text/css" href="../../normalize.css"> <link rel="stylesheet" type="text/css" href="../../rustdoc.css"> <link rel="stylesheet" type="text/css" href="../../main.css"> <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"> <a href='../../std/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="block items"><ul><li><a href="#required-methods">Required Methods</a></li><li><a href="#implementors">Implementors</a></li></ul></div><p class='location'><a href='../index.html'>std</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> </nav> <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'>std</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'>−</span>] </a> </span><a class='srclink' href='../../src/core/ops.rs.html#220-241' title='goto source code'>[src]</a></span></h1> <pre class='rust trait'><div class="docblock attributes">#[lang = "drop"] </div>pub trait Drop { fn <a href='#tymethod.drop' class='fnname'>drop</a>(&mut self); }</pre><div class='docblock'><p>The <code>Drop</code> trait is 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, if it implements this trait, it will have its <code>drop</code> method called. Then any fields the value contains will also be dropped recursively.</p> <p>Because of the recursive dropping, you do not need to implement this trait unless your type needs its own destructor logic.</p> <h1 id='examples' class='section-header'><a href='#examples'>Examples</a></h1> <p>A trivial implementation of <code>Drop</code>. 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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) { <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Dropping!"</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=struct%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%0A">Run</a></pre> <p>Showing the recursive nature of <code>Drop</code>. 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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) { <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Dropping Inner!"</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">&</span><span class="kw-2">mut</span> <span class="self">self</span>) { <span class="macro">println</span><span class="macro">!</span>(<span class="string">"Dropping Outer!"</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=struct%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%0A">Run</a></pre> <p>Because variables are dropped in the reverse order they are declared, <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">&</span><span class="lifetime">'static</span> <span class="ident">str</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">"Declared first!"</span>); <span class="kw">let</span> <span class="ident">_second</span> <span class="op">=</span> <span class="ident">PrintOnDrop</span>(<span class="string">"Declared second!"</span>); }<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=struct%20PrintOnDrop(%26'static%20str)%3B%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%0A">Run</a></pre> </div> <h2 id='required-methods'>Required Methods</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>(&mut self)</code></span></h3><div class='docblock'><p>A method called when the value goes out of scope.</p> <p>When this method has been called, <code>self</code> has not yet been deallocated. If it were, <code>self</code> would be a dangling reference.</p> <p>After this function is over, the memory of <code>self</code> will be deallocated.</p> <p>This function 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> <h1 id='panics' class='section-header'><a href='#panics'>Panics</a></h1> <p>Given that a <code>panic!</code> will call <code>drop()</code> as it unwinds, any <code>panic!</code> in a <code>drop()</code> implementation will likely abort.</p> </div></div> <h2 id='implementors'>Implementors</h2> <ul class='item-list' id='implementors-list'> <li><code>impl<T> Drop for <a class="struct" href="../../std/boxed/struct.IntermediateBox.html" title="struct std::boxed::IntermediateBox">IntermediateBox</a><T> <span class="where fmt-newline">where<br> T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><code>impl<T> Drop for <a class="struct" href="../../std/rc/struct.Rc.html" title="struct std::rc::Rc">Rc</a><T> <span class="where fmt-newline">where<br> T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><code>impl<T> Drop for std::rc::<a class="struct" href="../../std/rc/struct.Weak.html" title="struct std::rc::Weak">Weak</a><T> <span class="where fmt-newline">where<br> T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><code>impl<T> Drop for <a class="struct" href="../../alloc/raw_vec/struct.RawVec.html" title="struct alloc::raw_vec::RawVec">RawVec</a><T></code></li> <li><code>impl<T> Drop for std::sync::<a class="struct" href="../../std/sync/struct.Weak.html" title="struct std::sync::Weak">Weak</a><T> <span class="where fmt-newline">where<br> T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><code>impl<T> Drop for <a class="struct" href="../../std/sync/struct.Arc.html" title="struct std::sync::Arc">Arc</a><T> <span class="where fmt-newline">where<br> T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><code>impl<T> Drop for <a class="struct" href="../../std/boxed/struct.Box.html" title="struct std::boxed::Box">Box</a><T> <span class="where fmt-newline">where<br> T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>, </span></code></li> <li><code>impl<T> Drop for std::vec::<a class="struct" href="../../std/vec/struct.IntoIter.html" title="struct std::vec::IntoIter">IntoIter</a><T></code></li> <li><code>impl<'a, T> Drop for std::collections::vec_deque::<a class="struct" href="../../std/collections/vec_deque/struct.Drain.html" title="struct std::collections::vec_deque::Drain">Drain</a><'a, T> <span class="where fmt-newline">where<br> T: 'a, </span></code></li> <li><code>impl<K, V> Drop for <a class="struct" href="../../std/collections/btree_map/struct.BTreeMap.html" title="struct std::collections::btree_map::BTreeMap">BTreeMap</a><K, V></code></li> <li><code>impl<K, V> Drop for std::collections::btree_map::<a class="struct" href="../../std/collections/btree_map/struct.IntoIter.html" title="struct std::collections::btree_map::IntoIter">IntoIter</a><K, V></code></li> <li><code>impl<'a, T> Drop for <a class="struct" href="../../std/collections/binary_heap/struct.PeekMut.html" title="struct std::collections::binary_heap::PeekMut">PeekMut</a><'a, T> <span class="where fmt-newline">where<br> T: <a class="trait" href="../../std/cmp/trait.Ord.html" title="trait std::cmp::Ord">Ord</a>, </span></code></li> <li><code>impl<T> Drop for <a class="struct" href="../../std/vec/struct.Vec.html" title="struct std::vec::Vec">Vec</a><T></code></li> <li><code>impl<'a, I> Drop for std::vec::<a class="struct" href="../../std/vec/struct.Splice.html" title="struct std::vec::Splice">Splice</a><'a, I> <span class="where fmt-newline">where<br> I: <a class="trait" href="../../std/iter/trait.Iterator.html" title="trait std::iter::Iterator">Iterator</a>, </span></code></li> <li><code>impl<T> Drop for <a class="struct" href="../../std/collections/linked_list/struct.LinkedList.html" title="struct std::collections::linked_list::LinkedList">LinkedList</a><T></code></li> <li><code>impl<'a, 'b> Drop for std::string::<a class="struct" href="../../std/string/struct.Splice.html" title="struct std::string::Splice">Splice</a><'a, 'b></code></li> <li><code>impl<'a, T> Drop for std::vec::<a class="struct" href="../../std/vec/struct.Drain.html" title="struct std::vec::Drain">Drain</a><'a, T></code></li> <li><code>impl<'a> Drop for std::string::<a class="struct" href="../../std/string/struct.Drain.html" title="struct std::string::Drain">Drain</a><'a></code></li> <li><code>impl<T> Drop for <a class="struct" href="../../std/collections/vec_deque/struct.VecDeque.html" title="struct std::collections::vec_deque::VecDeque">VecDeque</a><T></code></li> <li><code>impl<'a, K, V> Drop for <a class="struct" href="../../std/collections/hash_map/struct.EntryPlace.html" title="struct std::collections::hash_map::EntryPlace">EntryPlace</a><'a, K, V></code></li> <li><code>impl Drop for <a class="struct" href="../../std/ffi/struct.CString.html" title="struct std::ffi::CString">CString</a></code></li> <li><code>impl<W: <a class="trait" href="../../std/io/trait.Write.html" title="trait std::io::Write">Write</a>> Drop for <a class="struct" href="../../std/io/struct.BufWriter.html" title="struct std::io::BufWriter">BufWriter</a><W></code></li> <li><code>impl Drop for <a class="struct" href="../../std/sync/mpsc/struct.Select.html" title="struct std::sync::mpsc::Select">Select</a></code></li> <li><code>impl<'rx, T: <a class="trait" href="../../std/marker/trait.Send.html" title="trait std::marker::Send">Send</a>> Drop for <a class="struct" href="../../std/sync/mpsc/struct.Handle.html" title="struct std::sync::mpsc::Handle">Handle</a><'rx, T></code></li> <li><code>impl<T> Drop for <a class="struct" href="../../std/sync/mpsc/struct.Sender.html" title="struct std::sync::mpsc::Sender">Sender</a><T></code></li> <li><code>impl<T> Drop for <a class="struct" href="../../std/sync/mpsc/struct.SyncSender.html" title="struct std::sync::mpsc::SyncSender">SyncSender</a><T></code></li> <li><code>impl<T> Drop for <a class="struct" href="../../std/sync/mpsc/struct.Receiver.html" title="struct std::sync::mpsc::Receiver">Receiver</a><T></code></li> <li><code>impl Drop for <a class="struct" href="../../std/sync/struct.Condvar.html" title="struct std::sync::Condvar">Condvar</a></code></li> <li><code>impl<T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> Drop for <a class="struct" href="../../std/sync/struct.Mutex.html" title="struct std::sync::Mutex">Mutex</a><T></code></li> <li><code>impl<'a, T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> Drop for <a class="struct" href="../../std/sync/struct.MutexGuard.html" title="struct std::sync::MutexGuard">MutexGuard</a><'a, T></code></li> <li><code>impl<T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> Drop for <a class="struct" href="../../std/sync/struct.RwLock.html" title="struct std::sync::RwLock">RwLock</a><T></code></li> <li><code>impl<'a, T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> Drop for <a class="struct" href="../../std/sync/struct.RwLockReadGuard.html" title="struct std::sync::RwLockReadGuard">RwLockReadGuard</a><'a, T></code></li> <li><code>impl<'a, T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>> Drop for <a class="struct" href="../../std/sync/struct.RwLockWriteGuard.html" title="struct std::sync::RwLockWriteGuard">RwLockWriteGuard</a><'a, T></code></li> </ul><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>?</dt> <dd>Show this help dialog</dd> <dt>S</dt> <dd>Focus the search field</dd> <dt>⇤</dt> <dd>Move up in search results</dd> <dt>⇥</dt> <dd>Move down in search results</dd> <dt>⏎</dt> <dd>Go to active search result</dd> <dt>+</dt> <dd>Collapse/expand 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 = "std"; </script> <script src="../../main.js"></script> <script defer src="../../search-index.js"></script> </body> </html>