Sophie

Sophie

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

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">
    <title>Drop</title>

    <link rel="stylesheet" type="text/css" href="../rust.css">

    <link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico">


</head>
<body class="rustdoc">
    <!--[if lte IE 8]>
    <div class="warning">
        This old browser is unsupported and will most likely display funky
        things.
    </div>
    <![endif]-->

    <div id="versioninfo">
  <img src="https://www.rust-lang.org/logos/rust-logo-32x32-blk.png" width="32" height="32" alt="Rust logo"><br>
  <span class="white-sticker"><a href="https://www.rust-lang.org">Rust</a> 1.28.0</span><br>
  <a href="https://github.com/rust-lang/rust/commit/"
    class="hash white-sticker"></a>
</div>


    <h1 class="title">Drop</h1>
    <p><small>There is a new edition of the book and this is an old link.</small></p>
<blockquote>
<p><code>Drop</code> lets us customize what happens when a value is about to go out of scope.</p>
</blockquote>

<pre class="rust rust-example-rendered">
<span class="kw">struct</span> <span class="ident">CustomSmartPointer</span> {
    <span class="ident">data</span>: <span class="ident">String</span>,
}

<span class="kw">impl</span> <span class="ident">Drop</span> <span class="kw">for</span> <span class="ident">CustomSmartPointer</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 CustomSmartPointer with data `{}`!&quot;</span>, <span class="self">self</span>.<span class="ident">data</span>);
    }
}

<span class="kw">fn</span> <span class="ident">main</span>() {
    <span class="kw">let</span> <span class="ident">c</span> <span class="op">=</span> <span class="ident">CustomSmartPointer</span> { <span class="ident">data</span>: <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">&quot;my stuff&quot;</span>) };
    <span class="kw">let</span> <span class="ident">d</span> <span class="op">=</span> <span class="ident">CustomSmartPointer</span> { <span class="ident">data</span>: <span class="ident">String</span>::<span class="ident">from</span>(<span class="string">&quot;other stuff&quot;</span>) };
    <span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;CustomSmartPointers created.&quot;</span>);
}<a class="test-arrow" target="_blank" href="https://play.rust-lang.org/?code=%23!%5Ballow(unused)%5D%0Astruct%20CustomSmartPointer%20%7B%0A%20%20%20%20data%3A%20String%2C%0A%7D%0A%0Aimpl%20Drop%20for%20CustomSmartPointer%20%7B%0A%20%20%20%20fn%20drop(%26mut%20self)%20%7B%0A%20%20%20%20%20%20%20%20println!(%22Dropping%20CustomSmartPointer%20with%20data%20%60%7B%7D%60!%22%2C%20self.data)%3B%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20c%20%3D%20CustomSmartPointer%20%7B%20data%3A%20String%3A%3Afrom(%22my%20stuff%22)%20%7D%3B%0A%20%20%20%20let%20d%20%3D%20CustomSmartPointer%20%7B%20data%3A%20String%3A%3Afrom(%22other%20stuff%22)%20%7D%3B%0A%20%20%20%20println!(%22CustomSmartPointers%20created.%22)%3B%0A%7D">Run</a></pre>
<hr />
<p>Here are the relevant sections in the new and old books:</p>
<ul>
<li><strong><a href="second-edition/ch15-03-drop.html">In the second edition: Ch 15.03 — The <code>Drop</code> Trait Runs Code on Cleanup</a></strong></li>
<li><small><a href="first-edition/drop.html">In the first edition: Ch 3.20 — Drop</a></small></li>
</ul>

    <footer><p>
Copyright &copy; 2011 The Rust Project Developers. Licensed under the
<a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>
or the <a href="https://opensource.org/licenses/MIT">MIT license</a>, at your option.
</p><p>
This file may not be copied, modified, or distributed except according to those terms.
</p></footer>


</body>
</html>