You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

405 lines
68 KiB

1 month ago
<!DOCTYPE HTML>
<html lang="zh-CN" class="light" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Drop 释放资源 - Rust语言圣经(Rust Course)</title>
<!-- Custom HTML head -->
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../../favicon.svg">
<link rel="shortcut icon" href="../../favicon.png">
<link rel="stylesheet" href="../../css/variables.css">
<link rel="stylesheet" href="../../css/general.css">
<link rel="stylesheet" href="../../css/chrome.css">
<link rel="stylesheet" href="../../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../../highlight.css">
<link rel="stylesheet" href="../../tomorrow-night.css">
<link rel="stylesheet" href="../../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<link rel="stylesheet" href="../../theme/style.css">
</head>
<body class="sidebar-visible no-js">
<div id="body-container">
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('light')
html.classList.add(theme);
var body = document.querySelector('body');
body.classList.remove('no-js')
body.classList.add('js');
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var body = document.querySelector('body');
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
body.classList.remove('sidebar-visible');
body.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div class="sidebar-scrollbox">
<ol class="chapter"><li class="chapter-item affix "><a href="../../about-book.html">关于本书</a></li><li class="chapter-item affix "><a href="../../into-rust.html">进入 Rust 编程世界</a></li><li class="chapter-item affix "><a href="../../first-try/sth-you-should-not-do.html">避免从入门到放弃</a></li><li class="chapter-item affix "><a href="../../community.html">社区和锈书</a></li><li class="spacer"></li><li class="chapter-item affix "><a href="../../some-thoughts.html">Xobserve: 一切皆可观测</a></li><li class="chapter-item affix "><a href="../../beat-ai.html">BeatAI: 工程师 AI 入门圣经</a></li><li class="chapter-item affix "><li class="part-title">Rust 语言基础学习</li><li class="spacer"></li><li class="chapter-item "><a href="../../first-try/intro.html"><strong aria-hidden="true">1.</strong> 寻找牛刀,以便小试</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../first-try/installation.html"><strong aria-hidden="true">1.1.</strong> 安装 Rust 环境</a></li><li class="chapter-item "><a href="../../first-try/editor.html"><strong aria-hidden="true">1.2.</strong> 墙推 VSCode!</a></li><li class="chapter-item "><a href="../../first-try/cargo.html"><strong aria-hidden="true">1.3.</strong> 认识 Cargo</a></li><li class="chapter-item "><a href="../../first-try/hello-world.html"><strong aria-hidden="true">1.4.</strong> 不仅仅是 Hello world</a></li><li class="chapter-item "><a href="../../first-try/slowly-downloading.html"><strong aria-hidden="true">1.5.</strong> 下载依赖太慢了?</a></li></ol></li><li class="chapter-item "><a href="../../basic/intro.html"><strong aria-hidden="true">2.</strong> Rust 基础入门</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/variable.html"><strong aria-hidden="true">2.1.</strong> 变量绑定与解构</a></li><li class="chapter-item "><a href="../../basic/base-type/index.html"><strong aria-hidden="true">2.2.</strong> 基本类型</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/base-type/numbers.html"><strong aria-hidden="true">2.2.1.</strong> 数值类型</a></li><li class="chapter-item "><a href="../../basic/base-type/char-bool.html"><strong aria-hidden="true">2.2.2.</strong> 字符、布尔、单元类型</a></li><li class="chapter-item "><a href="../../basic/base-type/statement-expression.html"><strong aria-hidden="true">2.2.3.</strong> 语句与表达式</a></li><li class="chapter-item "><a href="../../basic/base-type/function.html"><strong aria-hidden="true">2.2.4.</strong> 函数</a></li></ol></li><li class="chapter-item "><a href="../../basic/ownership/index.html"><strong aria-hidden="true">2.3.</strong> 所有权和借用</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/ownership/ownership.html"><strong aria-hidden="true">2.3.1.</strong> 所有权</a></li><li class="chapter-item "><a href="../../basic/ownership/borrowing.html"><strong aria-hidden="true">2.3.2.</strong> 引用与借用</a></li></ol></li><li class="chapter-item "><a href="../../basic/compound-type/intro.html"><strong aria-hidden="true">2.4.</strong> 复合类型</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../basic/compound-type/string-slice.html"><strong aria-hidden="true">2.4.1.</strong> 字符串与切片</a></li><li class="chapter-item "><a href="../../basic/compound-type/tuple.html"><strong aria-hidden="true">2.4.2.</strong> 元组</a></li><li class="chapter-item "><a href="../../basic/compound-type/struct.html"><strong aria-hidden="true">2.4.3.</strong> 结构体</a></li><li class="chapter-item "><a href="../../basic/compound-type/enum.html"><strong aria-hidden="true">2.4.4.</strong> 枚举</a></li><li class="chapter-item "><a href="../../basic/compound-type/array.html"><strong aria-hidden="true">2.4.5.</strong> 数组</a></l
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<!-- Track and set sidebar scroll position -->
<script>
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
sidebarScrollbox.addEventListener('click', function(e) {
if (e.target.tagName === 'A') {
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
}
}, { passive: true });
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
sessionStorage.removeItem('sidebar-scroll');
if (sidebarScrollTop) {
// preserve sidebar scroll position when navigating via links within sidebar
sidebarScrollbox.scrollTop = sidebarScrollTop;
} else {
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
var activeSection = document.querySelector('#sidebar .active');
if (activeSection) {
activeSection.scrollIntoView({ block: 'center' });
}
}
</script>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Rust语言圣经(Rust Course)</h1>
<div class="right-buttons">
<a href="../../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/sunface/rust-course" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/sunface/rust-course/edit/main/src/advance/smart-pointer/drop.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<!-- Page table of contents -->
<div class="sidetoc"><nav class="pagetoc"></nav></div>
<main>
<h1 id="drop-释放资源"><a class="header" href="#drop-释放资源">Drop 释放资源</a></h1>
<p>在 Rust 中,我们之所以可以一拳打跑 GC 的同时一脚踢翻手动资源回收,主要就归功于 <code>Drop</code> 特征,同时它也是智能指针的必备特征之一。</p>
<h2 id="学习目标"><a class="header" href="#学习目标">学习目标</a></h2>
<p>如何自动和手动释放资源及执行指定的收尾工作</p>
<h2 id="rust-中的资源回收"><a class="header" href="#rust-中的资源回收">Rust 中的资源回收</a></h2>
<p>在一些无 GC 语言中,程序员在一个变量无需再被使用时,需要手动释放它占用的内存资源,如果忘记了,那么就会发生内存泄漏,最终臭名昭著的 <code>OOM</code> 问题可能就会发生。</p>
<p>而在 Rust 中你可以指定在一个变量超出作用域时执行一段特定的代码最终编译器将帮你自动插入这段收尾代码。这样就无需在每一个使用该变量的地方都写一段代码来进行收尾工作和资源释放。不禁让人感叹Rust 的大腿真粗,香!</p>
<p>没错,指定这样一段收尾工作靠的就是咱这章的主角 - <code>Drop</code> 特征。</p>
<h2 id="一个不那么简单的-drop-例子"><a class="header" href="#一个不那么简单的-drop-例子">一个不那么简单的 Drop 例子</a></h2>
<pre><pre class="playground"><code class="language-rust edition2021">struct HasDrop1;
struct HasDrop2;
impl Drop for HasDrop1 {
fn drop(&amp;mut self) {
println!(&quot;Dropping HasDrop1!&quot;);
}
}
impl Drop for HasDrop2 {
fn drop(&amp;mut self) {
println!(&quot;Dropping HasDrop2!&quot;);
}
}
struct HasTwoDrops {
one: HasDrop1,
two: HasDrop2,
}
impl Drop for HasTwoDrops {
fn drop(&amp;mut self) {
println!(&quot;Dropping HasTwoDrops!&quot;);
}
}
struct Foo;
impl Drop for Foo {
fn drop(&amp;mut self) {
println!(&quot;Dropping Foo!&quot;)
}
}
fn main() {
let _x = HasTwoDrops {
two: HasDrop2,
one: HasDrop1,
};
let _foo = Foo;
println!(&quot;Running!&quot;);
}</code></pre></pre>
<p>上面代码虽然长,但是目的其实很单纯,就是为了观察不同情况下变量级别的、结构体内部字段的 <code>Drop</code>,有几点值得注意:</p>
<ul>
<li><code>Drop</code> 特征中的 <code>drop</code> 方法借用了目标的可变引用,而不是拿走了所有权,这里先设置一个悬念,后边会讲</li>
<li>结构体中每个字段都有自己的 <code>Drop</code></li>
</ul>
<p>来看看输出:</p>
<pre><code class="language-console">Running!
Dropping Foo!
Dropping HasTwoDrops!
Dropping HasDrop1!
Dropping HasDrop2!
</code></pre>
<p>嗯,结果符合预期,每个资源都成功的执行了收尾工作,虽然 <code>println!</code> 这种收尾工作毫无意义 =,=</p>
<h4 id="drop-的顺序"><a class="header" href="#drop-的顺序">Drop 的顺序</a></h4>
<p>观察以上输出,我们可以得出以下关于 <code>Drop</code> 顺序的结论</p>
<ul>
<li><strong>变量级别,按照逆序的方式</strong><code>_x</code><code>_foo</code> 之前创建,因此 <code>_x</code><code>_foo</code> 之后被 <code>drop</code></li>
<li><strong>结构体内部,按照顺序的方式</strong>,结构体 <code>_x</code> 中的字段按照定义中的顺序依次 <code>drop</code></li>
</ul>
<h4 id="没有实现-drop-的结构体"><a class="header" href="#没有实现-drop-的结构体">没有实现 Drop 的结构体</a></h4>
<p>实际上,就算你不为 <code>_x</code> 结构体实现 <code>Drop</code> 特征,它内部的两个字段依然会调用 <code>drop</code>,移除以下代码,并观察输出:</p>
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>impl Drop for HasTwoDrops {
fn drop(&amp;mut self) {
println!(&quot;Dropping HasTwoDrops!&quot;);
}
}
<span class="boring">}</span></code></pre></pre>
<p>原因在于Rust 自动为几乎所有类型都实现了 <code>Drop</code> 特征,因此就算你不手动为结构体实现 <code>Drop</code>,它依然会调用默认实现的 <code>drop</code> 函数,同时再调用每个字段的 <code>drop</code> 方法,最终打印出:</p>
<pre><code class="language-cnosole">Dropping HasDrop1!
Dropping HasDrop2!
</code></pre>
<h2 id="手动回收"><a class="header" href="#手动回收">手动回收</a></h2>
<p>当使用智能指针来管理锁的时候,你可能希望提前释放这个锁,然后让其它代码能及时获得锁,此时就需要提前去手动 <code>drop</code>
但是在之前我们提到一个悬念,<code>Drop::drop</code> 只是借用了目标值的可变引用,所以,就算你提前调用了 <code>drop</code>,后面的代码依然可以使用目标值,但是这就会访问一个并不存在的值,非常不安全,好在 Rust 会阻止你:</p>
<pre><pre class="playground"><code class="language-rust edition2021">#[derive(Debug)]
struct Foo;
impl Drop for Foo {
fn drop(&amp;mut self) {
println!(&quot;Dropping Foo!&quot;)
}
}
fn main() {
let foo = Foo;
foo.drop();
println!(&quot;Running!:{:?}&quot;, foo);
}</code></pre></pre>
<p>报错如下:</p>
<pre><code class="language-console">error[E0040]: explicit use of destructor method
--&gt; src/main.rs:37:9
|
37 | foo.drop();
| ----^^^^--
| | |
| | explicit destructor calls not allowed
| help: consider using `drop` function: `drop(foo)`
</code></pre>
<p>如上所示,编译器直接阻止了我们调用 <code>Drop</code> 特征的 <code>drop</code> 方法,原因是对于 Rust 而言,不允许显式地调用析构函数(这是一个用来清理实例的通用编程概念)。好在在报错的同时,编译器还给出了一个提示:使用 <code>drop</code> 函数。</p>
<p>针对编译器提示的 <code>drop</code> 函数,我们可以大胆推测下:它能够拿走目标值的所有权。现在来看看这个猜测正确与否,以下是 <code>std::mem::drop</code> 函数的签名:</p>
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>pub fn drop&lt;T&gt;(_x: T)
<span class="boring">}</span></code></pre></pre>
<p>如上所示,<code>drop</code> 函数确实拿走了目标值的所有权,来验证下:</p>
<pre><pre class="playground"><code class="language-rust edition2021">fn main() {
let foo = Foo;
drop(foo);
// 以下代码会报错:借用了所有权被转移的值
// println!(&quot;Running!:{:?}&quot;, foo);
}</code></pre></pre>
<p>Bingo完美拿走了所有权而且这种实现保证了后续的使用必定会导致编译错误因此非常安全</p>
<p>细心的同学可能已经注意到,这里直接调用了 <code>drop</code> 函数,并没有引入任何模块信息,原因是该函数在<a href="https://course.rs/appendix/prelude.html"><code>std::prelude</code></a>里。</p>
<blockquote>
<p>事实上能被显式调用的drop(_x)函数只是个空函数在拿走目标值的所有权后没有任何操作。而由于其持有目标值的所有权在drop(_x)函数结束之际编译器会执行_x真正的析构函数从而完成释放资源的操作。换句话说drop(_x)函数只是帮助目标值的所有者提前离开了作用域。https://doc.rust-lang.org/std/mem/fn.drop.html</p>
</blockquote>
<h2 id="drop-使用场景"><a class="header" href="#drop-使用场景">Drop 使用场景</a></h2>
<p>对于 Drop 而言,主要有两个功能:</p>
<ul>
<li>回收内存资源</li>
<li>执行一些收尾工作</li>
</ul>
<p>对于第二点,在之前我们已经详细介绍过,因此这里主要对第一点进行下简单说明。</p>
<p>在绝大多数情况下,我们都无需手动去 <code>drop</code> 以回收内存资源,因为 Rust 会自动帮我们完成这些工作,它甚至会对复杂类型的每个字段都单独的调用 <code>drop</code> 进行回收!但是确实有极少数情况,需要你自己来回收资源的,例如文件描述符、网络 socket 等,当这些值超出作用域不再使用时,就需要进行关闭以释放相关的资源,在这些情况下,就需要使用者自己来解决 <code>Drop</code> 的问题。</p>
<h2 id="互斥的-copy-和-drop"><a class="header" href="#互斥的-copy-和-drop">互斥的 Copy 和 Drop</a></h2>
<p>我们无法为一个类型同时实现 <code>Copy</code><code>Drop</code> 特征。因为实现了 <code>Copy</code> 特征的类型会被编译器隐式的复制,因此非常难以预测析构函数执行的时间和频率。因此这些实现了 <code>Copy</code> 的类型无法拥有析构函数。</p>
<pre><pre class="playground"><code class="language-rust edition2021"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>#[derive(Copy)]
struct Foo;
impl Drop for Foo {
fn drop(&amp;mut self) {
println!(&quot;Dropping Foo!&quot;)
}
}
<span class="boring">}</span></code></pre></pre>
<p>以上代码报错如下:</p>
<pre><code class="language-console">error[E0184]: the trait `Copy` may not be implemented for this type; the type has a destructor
--&gt; src/main.rs:24:10
|
24 | #[derive(Copy)]
| ^^^^ Copy not allowed on types with destructors
</code></pre>
<h2 id="总结"><a class="header" href="#总结">总结</a></h2>
<p><code>Drop</code> 可以用于许多方面,来使得资源清理及收尾工作变得方便和安全,甚至可以用其创建我们自己的内存分配器!通过 <code>Drop</code> 特征和 Rust 所有权系统你无需担心之后的代码清理Rust 会自动考虑这些问题。</p>
<p>我们也无需担心意外的清理掉仍在使用的值,这会造成编译器错误:所有权系统确保引用总是有效的,也会确保 <code>drop</code> 只会在值不再被使用时被调用一次。</p>
<div id="giscus-container"></div>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../../advance/smart-pointer/deref.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../advance/smart-pointer/rc-arc.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../../advance/smart-pointer/deref.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../advance/smart-pointer/rc-arc.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="../../ace.js"></script>
<script src="../../editor.js"></script>
<script src="../../mode-rust.js"></script>
<script src="../../theme-dawn.js"></script>
<script src="../../theme-tomorrow_night.js"></script>
<script src="../../elasticlunr.min.js"></script>
<script src="../../mark.min.js"></script>
<script src="../../searcher.js"></script>
<script src="../../clipboard.min.js"></script>
<script src="../../highlight.js"></script>
<script src="../../book.js"></script>
<script type="text/javascript" charset="utf-8">
var pagePath = "advance/smart-pointer/drop.md"
</script>
<!-- Custom JS scripts -->
<script src="../../assets/custom.js"></script>
<script src="../../assets/bigPicture.js"></script>
</div>
</body>
</html>