Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-release > by-pkgid > 0c2243f8a1696816431e7210e991fa52 > files > 16554

rust-doc-1.35.0-1.mga7.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_in_place` fn in crate `std`."><meta name="keywords" content="rust, rustlang, rust-lang, drop_in_place"><title>std::ptr::drop_in_place - Rust</title><link rel="stylesheet" type="text/css" href="../../normalize1.35.0.css"><link rel="stylesheet" type="text/css" href="../../rustdoc1.35.0.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../../dark1.35.0.css"><link rel="stylesheet" type="text/css" href="../../light1.35.0.css" id="themeStyle"><script src="../../storage1.35.0.js"></script><noscript><link rel="stylesheet" href="../../noscript1.35.0.css"></noscript><link rel="shortcut icon" href="../../favicon1.35.0.ico"><style type="text/css">#crate-search{background-image:url("../../down-arrow1.35.0.svg");}</style></head><body class="rustdoc fn"><!--[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='../../std/index.html'><img src='../../rust-logo1.35.0.png' alt='logo' width='100'></a><div class="sidebar-elems"><p class='location'><a href='../index.html'>std</a>::<wbr><a href='index.html'>ptr</a></p><script>window.sidebarCurrent = {name: 'drop_in_place', ty: 'fn', 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="../../brush1.35.0.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../../theme1.35.0.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../../settings.html"><img src="../../wheel1.35.0.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span class='since' title='Stable since Rust version 1.8.0'>1.8.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/ptr.rs.html#182-184' title='goto source code'>[src]</a></span><span class='in-band'>Function <a href='../index.html'>std</a>::<wbr><a href='index.html'>ptr</a>::<wbr><a class="fn" href=''>drop_in_place</a></span></h1><pre class='rust fn'>pub unsafe fn drop_in_place&lt;T&gt;(to_drop: <a class="primitive" href="../primitive.pointer.html">*mut T</a>) <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: ?<a class="trait" href="../../std/marker/trait.Sized.html" title="trait std::marker::Sized">Sized</a>,&nbsp;</span></pre><div class='docblock'><p>Executes the destructor (if any) of the pointed-to value.</p>
<p>This is semantically equivalent to calling <a href="../ptr/fn.read.html"><code>ptr::read</code></a> and discarding
the result, but has the following advantages:</p>
<ul>
<li>
<p>It is <em>required</em> to use <code>drop_in_place</code> to drop unsized types like
trait objects, because they can't be read out onto the stack and
dropped normally.</p>
</li>
<li>
<p>It is friendlier to the optimizer to do this over <a href="../ptr/fn.read.html"><code>ptr::read</code></a> when
dropping manually allocated memory (e.g., when writing Box/Rc/Vec),
as the compiler doesn't need to prove that it's sound to elide the
copy.</p>
</li>
</ul>
<h1 id="safety" class="section-header"><a href="#safety">Safety</a></h1>
<p>Behavior is undefined if any of the following conditions are violated:</p>
<ul>
<li>
<p><code>to_drop</code> must be <a href="../ptr/index.html#safety">valid</a> for reads.</p>
</li>
<li>
<p><code>to_drop</code> must be properly aligned. See the example below for how to drop
an unaligned pointer.</p>
</li>
</ul>
<p>Additionally, if <code>T</code> is not <a href="../marker/trait.Copy.html"><code>Copy</code></a>, using the pointed-to value after
calling <code>drop_in_place</code> can cause undefined behavior. Note that <code>*to_drop = foo</code> counts as a use because it will cause the value to be dropped
again. <a href="../ptr/fn.write.html"><code>write</code></a> can be used to overwrite data without causing it to be
dropped.</p>
<p>Note that even if <code>T</code> has size <code>0</code>, the pointer must be non-NULL and properly aligned.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Manually remove the last item from a vector:</p>

<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">ptr</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">rc</span>::<span class="ident">Rc</span>;

<span class="kw">let</span> <span class="ident">last</span> <span class="op">=</span> <span class="ident">Rc</span>::<span class="ident">new</span>(<span class="number">1</span>);
<span class="kw">let</span> <span class="ident">weak</span> <span class="op">=</span> <span class="ident">Rc</span>::<span class="ident">downgrade</span>(<span class="kw-2">&amp;</span><span class="ident">last</span>);

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">v</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[<span class="ident">Rc</span>::<span class="ident">new</span>(<span class="number">0</span>), <span class="ident">last</span>];

<span class="kw">unsafe</span> {
    <span class="comment">// Get a raw pointer to the last element in `v`.</span>
    <span class="kw">let</span> <span class="ident">ptr</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">v</span>[<span class="number">1</span>] <span class="kw">as</span> <span class="kw-2">*</span><span class="kw-2">mut</span> <span class="kw">_</span>;
    <span class="comment">// Shorten `v` to prevent the last item from being dropped. We do that first,</span>
    <span class="comment">// to prevent issues if the `drop_in_place` below panics.</span>
    <span class="ident">v</span>.<span class="ident">set_len</span>(<span class="number">1</span>);
    <span class="comment">// Without a call `drop_in_place`, the last item would never be dropped,</span>
    <span class="comment">// and the memory it manages would be leaked.</span>
    <span class="ident">ptr</span>::<span class="ident">drop_in_place</span>(<span class="ident">ptr</span>);
}

<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">v</span>, <span class="kw-2">&amp;</span>[<span class="number">0</span>.<span class="ident">into</span>()]);

<span class="comment">// Ensure that the last item was dropped.</span>
<span class="macro">assert</span><span class="macro">!</span>(<span class="ident">weak</span>.<span class="ident">upgrade</span>().<span class="ident">is_none</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%3Aptr%3B%0Ause%20std%3A%3Arc%3A%3ARc%3B%0A%0Alet%20last%20%3D%20Rc%3A%3Anew(1)%3B%0Alet%20weak%20%3D%20Rc%3A%3Adowngrade(%26last)%3B%0A%0Alet%20mut%20v%20%3D%20vec!%5BRc%3A%3Anew(0)%2C%20last%5D%3B%0A%0Aunsafe%20%7B%0A%20%20%20%20%2F%2F%20Get%20a%20raw%20pointer%20to%20the%20last%20element%20in%20%60v%60.%0A%20%20%20%20let%20ptr%20%3D%20%26mut%20v%5B1%5D%20as%20*mut%20_%3B%0A%20%20%20%20%2F%2F%20Shorten%20%60v%60%20to%20prevent%20the%20last%20item%20from%20being%20dropped.%20We%20do%20that%20first%2C%0A%20%20%20%20%2F%2F%20to%20prevent%20issues%20if%20the%20%60drop_in_place%60%20below%20panics.%0A%20%20%20%20v.set_len(1)%3B%0A%20%20%20%20%2F%2F%20Without%20a%20call%20%60drop_in_place%60%2C%20the%20last%20item%20would%20never%20be%20dropped%2C%0A%20%20%20%20%2F%2F%20and%20the%20memory%20it%20manages%20would%20be%20leaked.%0A%20%20%20%20ptr%3A%3Adrop_in_place(ptr)%3B%0A%7D%0A%0Aassert_eq!(v%2C%20%26%5B0.into()%5D)%3B%0A%0A%2F%2F%20Ensure%20that%20the%20last%20item%20was%20dropped.%0Aassert!(weak.upgrade().is_none())%3B%0A%7D">Run</a></pre></div>
<p>Unaligned values cannot be dropped in place, they must be copied to an aligned
location first:</p>

<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">ptr</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">mem</span>;

<span class="kw">unsafe</span> <span class="kw">fn</span> <span class="ident">drop_after_copy</span><span class="op">&lt;</span><span class="ident">T</span><span class="op">&gt;</span>(<span class="ident">to_drop</span>: <span class="kw-2">*</span><span class="kw-2">mut</span> <span class="ident">T</span>) {
    <span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">copy</span>: <span class="ident">T</span> <span class="op">=</span> <span class="ident">mem</span>::<span class="ident">uninitialized</span>();
    <span class="ident">ptr</span>::<span class="ident">copy</span>(<span class="ident">to_drop</span>, <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">copy</span>, <span class="number">1</span>);
    <span class="ident">drop</span>(<span class="ident">copy</span>);
}

<span class="attribute">#[<span class="ident">repr</span>(<span class="ident">packed</span>, <span class="ident">C</span>)]</span>
<span class="kw">struct</span> <span class="ident">Packed</span> {
    <span class="ident">_padding</span>: <span class="ident">u8</span>,
    <span class="ident">unaligned</span>: <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">i32</span><span class="op">&gt;</span>,
}

<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">p</span> <span class="op">=</span> <span class="ident">Packed</span> { <span class="ident">_padding</span>: <span class="number">0</span>, <span class="ident">unaligned</span>: <span class="macro">vec</span><span class="macro">!</span>[<span class="number">42</span>] };
<span class="kw">unsafe</span> {
    <span class="ident">drop_after_copy</span>(<span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">p</span>.<span class="ident">unaligned</span> <span class="kw">as</span> <span class="kw-2">*</span><span class="kw-2">mut</span> <span class="kw">_</span>);
    <span class="ident">mem</span>::<span class="ident">forget</span>(<span class="ident">p</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%3Aptr%3B%0Ause%20std%3A%3Amem%3B%0A%0Aunsafe%20fn%20drop_after_copy%3CT%3E(to_drop%3A%20*mut%20T)%20%7B%0A%20%20%20%20let%20mut%20copy%3A%20T%20%3D%20mem%3A%3Auninitialized()%3B%0A%20%20%20%20ptr%3A%3Acopy(to_drop%2C%20%26mut%20copy%2C%201)%3B%0A%20%20%20%20drop(copy)%3B%0A%7D%0A%0A%23%5Brepr(packed%2C%20C)%5D%0Astruct%20Packed%20%7B%0A%20%20%20%20_padding%3A%20u8%2C%0A%20%20%20%20unaligned%3A%20Vec%3Ci32%3E%2C%0A%7D%0A%0Alet%20mut%20p%20%3D%20Packed%20%7B%20_padding%3A%200%2C%20unaligned%3A%20vec!%5B42%5D%20%7D%3B%0Aunsafe%20%7B%0A%20%20%20%20drop_after_copy(%26mut%20p.unaligned%20as%20*mut%20_)%3B%0A%20%20%20%20mem%3A%3Aforget(p)%3B%0A%7D%0A%7D">Run</a></pre></div>
<p>Notice that the compiler performs this copy automatically when dropping packed structs,
i.e., you do not usually have to worry about such issues unless you call <code>drop_in_place</code>
manually.</p>
</div></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 = "std";</script><script src="../../aliases.js"></script><script src="../../main1.35.0.js"></script><script defer src="../../search-index.js"></script></body></html>