|
|
<!DOCTYPE HTML>
|
|
|
<html lang="en" class="light sidebar-visible" dir="ltr">
|
|
|
<head>
|
|
|
<!-- Book generated using mdBook -->
|
|
|
<meta charset="UTF-8">
|
|
|
<title>引用循环会导致内存泄漏 - Rust 程序设计语言 简体中文版</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-de23e50b.svg">
|
|
|
<link rel="shortcut icon" href="favicon-8114d1fc.png">
|
|
|
<link rel="stylesheet" href="css/variables-8adf115d.css">
|
|
|
<link rel="stylesheet" href="css/general-2459343d.css">
|
|
|
<link rel="stylesheet" href="css/chrome-ae938929.css">
|
|
|
<link rel="stylesheet" href="css/print-9e4910d8.css" media="print">
|
|
|
|
|
|
<!-- Fonts -->
|
|
|
<link rel="stylesheet" href="fonts/fonts-9644e21d.css">
|
|
|
|
|
|
<!-- Highlight.js Stylesheets -->
|
|
|
<link rel="stylesheet" id="mdbook-highlight-css" href="highlight-493f70e1.css">
|
|
|
<link rel="stylesheet" id="mdbook-tomorrow-night-css" href="tomorrow-night-4c0ae647.css">
|
|
|
<link rel="stylesheet" id="mdbook-ayu-highlight-css" href="ayu-highlight-3fdfc3ac.css">
|
|
|
|
|
|
<!-- Custom theme stylesheets -->
|
|
|
<link rel="stylesheet" href="ferris-d33b75bf.css">
|
|
|
<link rel="stylesheet" href="theme/2018-edition-4e126c62.css">
|
|
|
<link rel="stylesheet" href="theme/semantic-notes-9b5766c0.css">
|
|
|
<link rel="stylesheet" href="theme/listing-cab26221.css">
|
|
|
|
|
|
|
|
|
<!-- Provide site root and default themes to javascript -->
|
|
|
<script>
|
|
|
const path_to_root = "";
|
|
|
const default_light_theme = "light";
|
|
|
const default_dark_theme = "navy";
|
|
|
window.path_to_searchindex_js = "searchindex-d5741f6a.js";
|
|
|
</script>
|
|
|
<!-- Start loading toc.js asap -->
|
|
|
<script src="toc-9d5d6d0e.js"></script>
|
|
|
</head>
|
|
|
<body>
|
|
|
<div id="mdbook-help-container">
|
|
|
<div id="mdbook-help-popup">
|
|
|
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
|
|
|
<div>
|
|
|
<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>
|
|
|
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
|
|
|
<p>Press <kbd>?</kbd> to show this help</p>
|
|
|
<p>Press <kbd>Esc</kbd> to hide this help</p>
|
|
|
</div>
|
|
|
</div>
|
|
|
</div>
|
|
|
<div id="mdbook-body-container">
|
|
|
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
|
|
<script>
|
|
|
try {
|
|
|
let theme = localStorage.getItem('mdbook-theme');
|
|
|
let 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>
|
|
|
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
|
|
|
let theme;
|
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
|
|
const html = document.documentElement;
|
|
|
html.classList.remove('light')
|
|
|
html.classList.add(theme);
|
|
|
html.classList.add("js");
|
|
|
</script>
|
|
|
|
|
|
<input type="checkbox" id="mdbook-sidebar-toggle-anchor" class="hidden">
|
|
|
|
|
|
<!-- Hide / unhide sidebar before it is displayed -->
|
|
|
<script>
|
|
|
let sidebar = null;
|
|
|
const sidebar_toggle = document.getElementById("mdbook-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 = false;
|
|
|
}
|
|
|
if (sidebar === 'visible') {
|
|
|
sidebar_toggle.checked = true;
|
|
|
} else {
|
|
|
html.classList.remove('sidebar-visible');
|
|
|
}
|
|
|
</script>
|
|
|
|
|
|
<nav id="mdbook-sidebar" class="sidebar" aria-label="Table of contents">
|
|
|
<!-- populated by js -->
|
|
|
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
|
|
|
<noscript>
|
|
|
<iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
|
|
|
</noscript>
|
|
|
<div id="mdbook-sidebar-resize-handle" class="sidebar-resize-handle">
|
|
|
<div class="sidebar-resize-indicator"></div>
|
|
|
</div>
|
|
|
</nav>
|
|
|
|
|
|
<div id="mdbook-page-wrapper" class="page-wrapper">
|
|
|
|
|
|
<div class="page">
|
|
|
<div id="mdbook-menu-bar-hover-placeholder"></div>
|
|
|
<div id="mdbook-menu-bar" class="menu-bar sticky">
|
|
|
<div class="left-buttons">
|
|
|
<label id="mdbook-sidebar-toggle" class="icon-button" for="mdbook-sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="mdbook-sidebar">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M0 96C0 78.3 14.3 64 32 64H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32C14.3 128 0 113.7 0 96zM0 256c0-17.7 14.3-32 32-32H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32c-17.7 0-32-14.3-32-32zM448 416c0 17.7-14.3 32-32 32H32c-17.7 0-32-14.3-32-32s14.3-32 32-32H416c17.7 0 32 14.3 32 32z"/></svg></span>
|
|
|
</label>
|
|
|
<button id="mdbook-theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="mdbook-theme-list">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M371.3 367.1c27.3-3.9 51.9-19.4 67.2-42.9L600.2 74.1c12.6-19.5 9.4-45.3-7.6-61.2S549.7-4.4 531.1 9.6L294.4 187.2c-24 18-38.2 46.1-38.4 76.1L371.3 367.1zm-19.6 25.4l-116-104.4C175.9 290.3 128 339.6 128 400c0 3.9 .2 7.8 .6 11.6c1.8 17.5-10.2 36.4-27.8 36.4H96c-17.7 0-32 14.3-32 32s14.3 32 32 32H240c61.9 0 112-50.1 112-112c0-2.5-.1-5-.2-7.5z"/></svg></span>
|
|
|
</button>
|
|
|
<ul id="mdbook-theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-default_theme">Auto</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-light">Light</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-rust">Rust</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-coal">Coal</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-navy">Navy</button></li>
|
|
|
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-ayu">Ayu</button></li>
|
|
|
</ul>
|
|
|
<button id="mdbook-search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="mdbook-searchbar">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352c79.5 0 144-64.5 144-144s-64.5-144-144-144S64 128.5 64 208s64.5 144 144 144z"/></svg></span>
|
|
|
</button>
|
|
|
</div>
|
|
|
|
|
|
<h1 class="menu-title">Rust 程序设计语言 简体中文版</h1>
|
|
|
|
|
|
<div class="right-buttons">
|
|
|
<a href="print.html" title="Print this book" aria-label="Print this book">
|
|
|
<span class=fa-svg id="print-button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M128 0C92.7 0 64 28.7 64 64v96h64V64H354.7L384 93.3V160h64V93.3c0-17-6.7-33.3-18.7-45.3L400 18.7C388 6.7 371.7 0 354.7 0H128zM384 352v32 64H128V384 368 352H384zm64 32h32c17.7 0 32-14.3 32-32V256c0-35.3-28.7-64-64-64H64c-35.3 0-64 28.7-64 64v96c0 17.7 14.3 32 32 32H64v64c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V384zm-16-88c-13.3 0-24-10.7-24-24s10.7-24 24-24s24 10.7 24 24s-10.7 24-24 24z"/></svg></span>
|
|
|
</a>
|
|
|
<a href="https://github.com/KaiserY/trpl-zh-cn/tree/main" title="Git repository" aria-label="Git repository">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<div id="mdbook-search-wrapper" class="hidden">
|
|
|
<form id="mdbook-searchbar-outer" class="searchbar-outer">
|
|
|
<div class="search-wrapper">
|
|
|
<input type="search" id="mdbook-searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="mdbook-searchresults-outer" aria-describedby="searchresults-header">
|
|
|
<div class="spinner-wrapper">
|
|
|
<span class=fa-svg id="fa-spin"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M304 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zm0 416c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM48 304c26.5 0 48-21.5 48-48s-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48zm464-48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM142.9 437c18.7-18.7 18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zm0-294.2c18.7-18.7 18.7-49.1 0-67.9S93.7 56.2 75 75s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zM369.1 437c18.7 18.7 49.1 18.7 67.9 0s18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9z"/></svg></span>
|
|
|
</div>
|
|
|
</div>
|
|
|
</form>
|
|
|
<div id="mdbook-searchresults-outer" class="searchresults-outer hidden">
|
|
|
<div id="mdbook-searchresults-header" class="searchresults-header"></div>
|
|
|
<ul id="mdbook-searchresults">
|
|
|
</ul>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
|
|
<script>
|
|
|
document.getElementById('mdbook-sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
|
|
document.getElementById('mdbook-sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
|
|
Array.from(document.querySelectorAll('#mdbook-sidebar a')).forEach(function(link) {
|
|
|
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
|
|
});
|
|
|
</script>
|
|
|
|
|
|
<div id="mdbook-content" class="content">
|
|
|
<main>
|
|
|
<h2 id="引用循环与内存泄漏"><a class="header" href="#引用循环与内存泄漏">引用循环与内存泄漏</a></h2>
|
|
|
<!-- https://github.com/rust-lang/book/blob/main/src/ch15-06-reference-cycles.md -->
|
|
|
<!-- commit 56ec353290429e6547109e88afea4de027b0f1a9 -->
|
|
|
<p>Rust 的内存安全性保证使其难以意外地制造永远也不会被清理的内存(被称为 <strong>内存泄漏</strong>,<em>memory leak</em>),但并非不可能。Rust 并不保证完全防止内存泄漏,这意味着内存泄漏在 Rust 中被认为是内存安全的。这一点可以通过 <code>Rc<T></code> 和 <code>RefCell<T></code> 看出 Rust 允许出现内存泄漏:创建引用循环的可能性是存在的。这会造成内存泄漏,因为每一项的引用计数永远也到不了 0,持有的数据也就永远不会被释放。</p>
|
|
|
<h3 id="制造引用循环"><a class="header" href="#制造引用循环">制造引用循环</a></h3>
|
|
|
<p>让我们看看引用循环是如何发生的以及如何避免它,以示例 15-25 中的 <code>List</code> 枚举和 <code>tail</code> 方法的定义开始:</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024">use crate::List::{Cons, Nil};
|
|
|
use std::cell::RefCell;
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
enum List {
|
|
|
Cons(i32, RefCell<Rc<List>>),
|
|
|
Nil,
|
|
|
}
|
|
|
|
|
|
impl List {
|
|
|
fn tail(&self) -> Option<&RefCell<Rc<List>>> {
|
|
|
match self {
|
|
|
Cons(_, item) => Some(item),
|
|
|
Nil => None,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
fn main() {}</code></pre>
|
|
|
<p><span class="caption">示例 15-25: 一个存放 <code>RefCell</code> 的 cons list 定义,这样可以修改 <code>Cons</code> 变体所引用的数据</span></p>
|
|
|
<p>这里采用了示例 15-5 中 <code>List</code> 定义的另一种变体。现在 <code>Cons</code> 变体的第二个元素是 <code>RefCell<Rc<List>></code>,这意味着不同于像示例 15-24 那样能够修改 <code>i32</code> 的值,我们希望能够修改 <code>Cons</code> 变体所指向的 <code>List</code>。这里还增加了一个 <code>tail</code> 方法来方便我们在有 <code>Cons</code> 变体的时候访问其第二项。</p>
|
|
|
<p>在示例 15-26 中增加了一个 <code>main</code> 函数,其使用了示例 15-25 中的定义。这些代码在 <code>a</code> 中创建了一个列表,一个指向 <code>a</code> 中列表的 <code>b</code> 列表,接着修改 <code>a</code> 中的列表指向 <code>b</code> 中的列表,这会创建一个引用循环。在这个过程的多个位置有 <code>println!</code> 语句展示引用计数。</p>
|
|
|
<p><span class="filename">文件:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">use crate::List::{Cons, Nil};
|
|
|
</span><span class="boring">use std::cell::RefCell;
|
|
|
</span><span class="boring">use std::rc::Rc;
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring">#[derive(Debug)]
|
|
|
</span><span class="boring">enum List {
|
|
|
</span><span class="boring"> Cons(i32, RefCell<Rc<List>>),
|
|
|
</span><span class="boring"> Nil,
|
|
|
</span><span class="boring">}
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring">impl List {
|
|
|
</span><span class="boring"> fn tail(&self) -> Option<&RefCell<Rc<List>>> {
|
|
|
</span><span class="boring"> match self {
|
|
|
</span><span class="boring"> Cons(_, item) => Some(item),
|
|
|
</span><span class="boring"> Nil => None,
|
|
|
</span><span class="boring"> }
|
|
|
</span><span class="boring"> }
|
|
|
</span><span class="boring">}
|
|
|
</span><span class="boring">
|
|
|
</span>fn main() {
|
|
|
let a = Rc::new(Cons(5, RefCell::new(Rc::new(Nil))));
|
|
|
|
|
|
println!("a initial rc count = {}", Rc::strong_count(&a));
|
|
|
println!("a next item = {:?}", a.tail());
|
|
|
|
|
|
let b = Rc::new(Cons(10, RefCell::new(Rc::clone(&a))));
|
|
|
|
|
|
println!("a rc count after b creation = {}", Rc::strong_count(&a));
|
|
|
println!("b initial rc count = {}", Rc::strong_count(&b));
|
|
|
println!("b next item = {:?}", b.tail());
|
|
|
|
|
|
if let Some(link) = a.tail() {
|
|
|
*link.borrow_mut() = Rc::clone(&b);
|
|
|
}
|
|
|
|
|
|
println!("b rc count after changing a = {}", Rc::strong_count(&b));
|
|
|
println!("a rc count after changing a = {}", Rc::strong_count(&a));
|
|
|
|
|
|
// Uncomment the next line to see that we have a cycle;
|
|
|
// it will overflow the stack.
|
|
|
// println!("a next item = {:?}", a.tail());
|
|
|
}</code></pre>
|
|
|
<p><span class="caption">示例 15-26:创建一个引用循环:两个 <code>List</code> 值互相指向彼此</span></p>
|
|
|
<p>这里在变量 <code>a</code> 中创建了一个 <code>Rc<List></code> 实例来存放初值为 <code>5, Nil</code> 的 <code>List</code> 值。接着在变量 <code>b</code> 中创建了存放包含值 <code>10</code> 和指向列表 <code>a</code> 的 <code>List</code> 的另一个 <code>Rc<List></code> 实例。</p>
|
|
|
<p>下来修改 <code>a</code> 使其指向 <code>b</code> 而不是 <code>Nil</code>,这就创建了一个循环。为此需要使用 <code>tail</code> 方法获取 <code>a</code> 中 <code>RefCell<Rc<List>></code> 的引用,并放入变量 <code>link</code> 中。接着使用 <code>RefCell<Rc<List>></code> 的 <code>borrow_mut</code> 方法将其值从存放 <code>Nil</code> 的 <code>Rc<List></code> 修改为 <code>b</code> 中的 <code>Rc<List></code>。</p>
|
|
|
<p>如果保持最后的 <code>println!</code> 行注释并运行代码,会得到如下输出:</p>
|
|
|
<pre><code class="language-console">$ cargo run
|
|
|
Compiling cons-list v0.1.0 (file:///projects/cons-list)
|
|
|
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.53s
|
|
|
Running `target/debug/cons-list`
|
|
|
a initial rc count = 1
|
|
|
a next item = Some(RefCell { value: Nil })
|
|
|
a rc count after b creation = 2
|
|
|
b initial rc count = 1
|
|
|
b next item = Some(RefCell { value: Cons(5, RefCell { value: Nil }) })
|
|
|
b rc count after changing a = 2
|
|
|
a rc count after changing a = 2
|
|
|
</code></pre>
|
|
|
<p>可以看到将列表 <code>a</code> 修改为指向 <code>b</code> 之后, <code>a</code> 和 <code>b</code> 中的 <code>Rc<List></code> 实例的引用计数都是 2。在 <code>main</code> 的结尾,Rust 丢弃 <code>b</code>,这会使 <code>b</code> <code>Rc<List></code> 实例的引用计数从 2 减为 1。此时该 <code>Rc<List></code> 实例并不会被回收,因为其引用计数是 1 而不是 0。接下来 Rust 会丢弃 <code>a</code> 将 <code>a</code> <code>Rc<List></code> 实例的引用计数从 2 减为 1。这个实例也不能被回收,由于另一个 <code>Rc<List></code> 实例依然引用它,所以其引用计数是 1。这些列表的内存将永远保持未被回收的状态。为了更直观地展示这一引用循环,我们创建了一个如图 15-4 所示的示意图:</p>
|
|
|
<img alt="Reference cycle of lists" src="img/trpl15-04.svg" class="center" />
|
|
|
<p><span class="caption">图 15-4: 列表 <code>a</code> 和 <code>b</code> 彼此互相指向形成引用循环</span></p>
|
|
|
<p>如果取消最后 <code>println!</code> 的注释并运行程序,Rust 会尝试打印出 <code>a</code> 指向 <code>b</code> 指向 <code>a</code> 这样的循环直到栈溢出。</p>
|
|
|
<p>相比真实世界的程序,这个例子中创建引用循环的结果并不可怕:创建了引用循环之后程序立刻就结束了。如果在更为复杂的程序中并在循环里分配了很多内存并占有很长时间,这个程序会使用多于它所需要的内存,并有可能压垮系统并造成没有内存可供使用。</p>
|
|
|
<p>创建引用循环并不容易,但也不是不可能。如果你有包含 <code>Rc<T></code> 的 <code>RefCell<T></code> 值或类似的嵌套结合了内部可变性和引用计数的类型,请务必小心确保你没有形成一个引用循环;你无法指望 Rust 帮你捕获它们。创建引用循环是一个程序上的逻辑 bug,你应该使用自动化测试、代码评审和其他软件开发最佳实践来使其最小化。</p>
|
|
|
<p>另一个解决方案是重新组织数据结构,使得一部分引用拥有所有权而另一部分没有。换句话说,循环将由一些拥有所有权的关系和一些无所有权的关系组成,只有所有权关系才能影响值是否可以被丢弃。在示例 15-25 中,我们总是希望 <code>Cons</code> 变体拥有其列表,所以重新组织数据结构是不可能的。让我们看看一个由父节点和子节点构成的图的例子,观察何时是使用无所有权的关系来避免引用循环的合适时机。</p>
|
|
|
<h3 id="使用-weakt-防止引用循环"><a class="header" href="#使用-weakt-防止引用循环">使用 <code>Weak<T></code> 防止引用循环</a></h3>
|
|
|
<p>到目前为止,我们已经展示了调用 <code>Rc::clone</code> 会增加 <code>Rc<T></code> 实例的 <code>strong_count</code>,和只在其 <code>strong_count</code> 为 0 时 <code>Rc<T></code> 实例才会被清理。你也可以通过调用 <code>Rc::downgrade</code> 并传递 <code>Rc<T></code> 实例的引用来创建其值的<strong>弱引用</strong>(<em>weak reference</em>)。强引用代表如何共享 <code>Rc<T></code> 实例的所有权;弱引用不表达所有权关系,当 <code>Rc<T></code> 实例被清理时其计数没有影响。它们不会造成引用循环,因为任何涉及弱引用的循环会在其相关的值的强引用计数为 0 时被打断。</p>
|
|
|
<p>调用 <code>Rc::downgrade</code> 时会得到 <code>Weak<T></code> 类型的智能指针。不同于将 <code>Rc<T></code> 实例的 <code>strong_count</code> 加 1,调用 <code>Rc::downgrade</code> 会将 <code>weak_count</code> 加 1。<code>Rc<T></code> 类型使用 <code>weak_count</code> 来记录其存在多少个 <code>Weak<T></code> 引用,类似于 <code>strong_count</code>。其区别在于 <code>weak_count</code> 无需计数为 0 就能使 <code>Rc<T></code> 实例被清理。</p>
|
|
|
<p>因为 <code>Weak<T></code> 引用的值可能已经被丢弃了,为了使用 <code>Weak<T></code> 所指向的值,我们必须确保其值仍然有效。为此可以调用 <code>Weak<T></code> 实例的 <code>upgrade</code> 方法,这会返回 <code>Option<Rc<T>></code>。如果 <code>Rc<T></code> 值还未被丢弃,则结果是 <code>Some</code>;如果 <code>Rc<T></code> 已被丢弃,则结果是 <code>None</code>。因为 <code>upgrade</code> 返回一个 <code>Option<Rc<T>></code>,Rust 会确保处理 <code>Some</code> 和 <code>None</code> 的情况,所以它不会返回无效指针。</p>
|
|
|
<p>作为示例,我们不再使用只知道下一个元素的列表,而是创建一个既知道子节点又知道父节点的树结构。</p>
|
|
|
<h4 id="创建树形数据结构带有子节点的-node"><a class="header" href="#创建树形数据结构带有子节点的-node">创建树形数据结构:带有子节点的 <code>Node</code></a></h4>
|
|
|
<p>首先,我们将构建一个节点能够知道其子节点的树。创建一个用于存放其拥有所有权的 <code>i32</code> 值和对其子 <code>Node</code> 的引用:</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024">use std::cell::RefCell;
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
struct Node {
|
|
|
value: i32,
|
|
|
children: RefCell<Vec<Rc<Node>>>,
|
|
|
}
|
|
|
<span class="boring">
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span><span class="boring"> let leaf = Rc::new(Node {
|
|
|
</span><span class="boring"> value: 3,
|
|
|
</span><span class="boring"> children: RefCell::new(vec![]),
|
|
|
</span><span class="boring"> });
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> let branch = Rc::new(Node {
|
|
|
</span><span class="boring"> value: 5,
|
|
|
</span><span class="boring"> children: RefCell::new(vec![Rc::clone(&leaf)]),
|
|
|
</span><span class="boring"> });
|
|
|
</span><span class="boring">}</span></code></pre>
|
|
|
<p>我们希望 <code>Node</code> 能够拥有其子节点,同时也希望能将所有权共享给变量,以便可以直接访问树中的每一个 <code>Node</code>,为此 <code>Vec<T></code> 的项的类型被定义为 <code>Rc<Node></code>。我们还希望能修改其他节点的子节点,所以 <code>children</code> 中 <code>Vec<Rc<Node>></code> 被放进了 <code>RefCell<T></code>。</p>
|
|
|
<p>接下来,使用此结构体定义来创建一个叫做 <code>leaf</code> 的带有值 <code>3</code> 且没有子节点的 <code>Node</code> 实例,和另一个带有值 5 并以 <code>leaf</code> 作为子节点的实例 <code>branch</code>,如示例 15-27 所示:</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">use std::cell::RefCell;
|
|
|
</span><span class="boring">use std::rc::Rc;
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring">#[derive(Debug)]
|
|
|
</span><span class="boring">struct Node {
|
|
|
</span><span class="boring"> value: i32,
|
|
|
</span><span class="boring"> children: RefCell<Vec<Rc<Node>>>,
|
|
|
</span><span class="boring">}
|
|
|
</span><span class="boring">
|
|
|
</span>fn main() {
|
|
|
let leaf = Rc::new(Node {
|
|
|
value: 3,
|
|
|
children: RefCell::new(vec![]),
|
|
|
});
|
|
|
|
|
|
let branch = Rc::new(Node {
|
|
|
value: 5,
|
|
|
children: RefCell::new(vec![Rc::clone(&leaf)]),
|
|
|
});
|
|
|
}</code></pre>
|
|
|
<p><span class="caption">示例 15-27:创建没有子节点的 <code>leaf</code> 节点和以 <code>leaf</code> 作为子节点的 <code>branch</code> 节点</span></p>
|
|
|
<p>这里克隆了 <code>leaf</code> 中的 <code>Rc<Node></code> 并储存在 <code>branch</code> 中,这意味着 <code>leaf</code> 中的 <code>Node</code> 现在有两个所有者:<code>leaf</code> 和 <code>branch</code>。可以通过 <code>branch.children</code> 从 <code>branch</code> 中获得 <code>leaf</code>,不过无法从 <code>leaf</code> 得到 <code>branch</code>。<code>leaf</code> 没有到 <code>branch</code> 的引用且并不知道它们相互关联。我们希望 <code>leaf</code> 知道 <code>branch</code> 是其父节点。接下来我们会这么做。</p>
|
|
|
<h4 id="增加从子到父的引用"><a class="header" href="#增加从子到父的引用">增加从子到父的引用</a></h4>
|
|
|
<p>为了使子节点知道其父节点,需要在 <code>Node</code> 结构体定义中增加一个 <code>parent</code> 字段。问题是 <code>parent</code> 的类型应该是什么。我们知道其不能包含 <code>Rc<T></code>,因为这样 <code>leaf.parent</code> 将会指向 <code>branch</code> 而 <code>branch.children</code> 会包含 <code>leaf</code> 的指针,这会形成引用循环,会造成其 <code>strong_count</code> 永远也不会为 0。</p>
|
|
|
<p>现在换一种方式思考这个关系,父节点应该拥有其子节点:如果父节点被丢弃了,其子节点也应该被丢弃。然而子节点不应该拥有其父节点:如果丢弃子节点,其父节点应该依然存在。这正是弱引用的例子!</p>
|
|
|
<p>所以 <code>parent</code> 使用 <code>Weak<T></code> 类型而不是 <code>Rc<T></code>,具体来说是 <code>RefCell<Weak<Node>></code>。现在 <code>Node</code> 结构体定义看起来像这样:</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024">use std::cell::RefCell;
|
|
|
use std::rc::{Rc, Weak};
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
struct Node {
|
|
|
value: i32,
|
|
|
parent: RefCell<Weak<Node>>,
|
|
|
children: RefCell<Vec<Rc<Node>>>,
|
|
|
}
|
|
|
<span class="boring">
|
|
|
</span><span class="boring">fn main() {
|
|
|
</span><span class="boring"> let leaf = Rc::new(Node {
|
|
|
</span><span class="boring"> value: 3,
|
|
|
</span><span class="boring"> parent: RefCell::new(Weak::new()),
|
|
|
</span><span class="boring"> children: RefCell::new(vec![]),
|
|
|
</span><span class="boring"> });
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> let branch = Rc::new(Node {
|
|
|
</span><span class="boring"> value: 5,
|
|
|
</span><span class="boring"> parent: RefCell::new(Weak::new()),
|
|
|
</span><span class="boring"> children: RefCell::new(vec![Rc::clone(&leaf)]),
|
|
|
</span><span class="boring"> });
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> *leaf.parent.borrow_mut() = Rc::downgrade(&branch);
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring"> println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
|
|
|
</span><span class="boring">}</span></code></pre>
|
|
|
<p>这样,一个节点就能够引用其父节点,但不拥有其父节点。在示例 15-28 中,我们更新 <code>main</code> 来使用新定义以便 <code>leaf</code> 节点可以通过 <code>branch</code> 引用其父节点:</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">use std::cell::RefCell;
|
|
|
</span><span class="boring">use std::rc::{Rc, Weak};
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring">#[derive(Debug)]
|
|
|
</span><span class="boring">struct Node {
|
|
|
</span><span class="boring"> value: i32,
|
|
|
</span><span class="boring"> parent: RefCell<Weak<Node>>,
|
|
|
</span><span class="boring"> children: RefCell<Vec<Rc<Node>>>,
|
|
|
</span><span class="boring">}
|
|
|
</span><span class="boring">
|
|
|
</span>fn main() {
|
|
|
let leaf = Rc::new(Node {
|
|
|
value: 3,
|
|
|
parent: RefCell::new(Weak::new()),
|
|
|
children: RefCell::new(vec![]),
|
|
|
});
|
|
|
|
|
|
println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
|
|
|
|
|
|
let branch = Rc::new(Node {
|
|
|
value: 5,
|
|
|
parent: RefCell::new(Weak::new()),
|
|
|
children: RefCell::new(vec![Rc::clone(&leaf)]),
|
|
|
});
|
|
|
|
|
|
*leaf.parent.borrow_mut() = Rc::downgrade(&branch);
|
|
|
|
|
|
println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
|
|
|
}</code></pre>
|
|
|
<p><span class="caption">示例 15-28:一个 <code>leaf</code> 节点,其拥有指向其父节点 <code>branch</code> 的弱引用</span></p>
|
|
|
<p>创建 <code>leaf</code> 节点类似于示例 15-27,除了 <code>parent</code> 字段有所不同:<code>leaf</code> 开始时没有父节点,所以我们新建了一个空的 <code>Weak<Node></code> 引用实例。</p>
|
|
|
<p>此时,当尝试使用 <code>upgrade</code> 方法获取 <code>leaf</code> 的父节点引用时,会得到一个 <code>None</code> 值。如第一个 <code>println!</code> 输出所示:</p>
|
|
|
<pre><code class="language-text">leaf parent = None
|
|
|
</code></pre>
|
|
|
<p>当创建 <code>branch</code> 节点时,其也会新建一个 <code>Weak<Node></code> 引用,因为 <code>branch</code> 并没有父节点。<code>leaf</code> 仍然作为 <code>branch</code> 的一个子节点。一旦在 <code>branch</code> 中有了 <code>Node</code> 实例,就可以修改 <code>leaf</code> 使其拥有指向父节点的 <code>Weak<Node></code> 引用。这里使用了 <code>leaf</code> 中 <code>parent</code> 字段里的 <code>RefCell<Weak<Node>></code> 的 <code>borrow_mut</code> 方法,接着使用了 <code>Rc::downgrade</code> 函数来从 <code>branch</code> 中的 <code>Rc<Node></code> 值创建了一个指向 <code>branch</code> 的 <code>Weak<Node></code> 引用。</p>
|
|
|
<p>当再次打印出 <code>leaf</code> 的父节点时,这一次将会得到存放了 <code>branch</code> 的 <code>Some</code> 值:现在 <code>leaf</code> 可以访问其父节点了!当打印出 <code>leaf</code> 时,我们也避免了如示例 15-26 中最终会导致栈溢出的循环:<code>Weak<Node></code> 引用被打印为 <code>(Weak)</code>:</p>
|
|
|
<pre><code class="language-text">leaf parent = Some(Node { value: 5, parent: RefCell { value: (Weak) },
|
|
|
children: RefCell { value: [Node { value: 3, parent: RefCell { value: (Weak) },
|
|
|
children: RefCell { value: [] } }] } })
|
|
|
</code></pre>
|
|
|
<p>没有无限的输出表明这段代码并没有造成引用循环。这一点也可以从观察 <code>Rc::strong_count</code> 和 <code>Rc::weak_count</code> 调用的结果看出。</p>
|
|
|
<h4 id="可视化-strong_count-和-weak_count-的改变"><a class="header" href="#可视化-strong_count-和-weak_count-的改变">可视化 <code>strong_count</code> 和 <code>weak_count</code> 的改变</a></h4>
|
|
|
<p>让我们通过创建了一个新的内部作用域并将 <code>branch</code> 的创建放入其中,来观察 <code>Rc<Node></code> 实例的 <code>strong_count</code> 和 <code>weak_count</code> 值的变化。这会展示当 <code>branch</code> 创建和离开作用域被丢弃时会发生什么。这些修改如示例 15-29 所示:</p>
|
|
|
<p><span class="filename">文件名:src/main.rs</span></p>
|
|
|
<pre class="playground"><code class="language-rust edition2024"><span class="boring">use std::cell::RefCell;
|
|
|
</span><span class="boring">use std::rc::{Rc, Weak};
|
|
|
</span><span class="boring">
|
|
|
</span><span class="boring">#[derive(Debug)]
|
|
|
</span><span class="boring">struct Node {
|
|
|
</span><span class="boring"> value: i32,
|
|
|
</span><span class="boring"> parent: RefCell<Weak<Node>>,
|
|
|
</span><span class="boring"> children: RefCell<Vec<Rc<Node>>>,
|
|
|
</span><span class="boring">}
|
|
|
</span><span class="boring">
|
|
|
</span>fn main() {
|
|
|
let leaf = Rc::new(Node {
|
|
|
value: 3,
|
|
|
parent: RefCell::new(Weak::new()),
|
|
|
children: RefCell::new(vec![]),
|
|
|
});
|
|
|
|
|
|
println!(
|
|
|
"leaf strong = {}, weak = {}",
|
|
|
Rc::strong_count(&leaf),
|
|
|
Rc::weak_count(&leaf),
|
|
|
);
|
|
|
|
|
|
{
|
|
|
let branch = Rc::new(Node {
|
|
|
value: 5,
|
|
|
parent: RefCell::new(Weak::new()),
|
|
|
children: RefCell::new(vec![Rc::clone(&leaf)]),
|
|
|
});
|
|
|
|
|
|
*leaf.parent.borrow_mut() = Rc::downgrade(&branch);
|
|
|
|
|
|
println!(
|
|
|
"branch strong = {}, weak = {}",
|
|
|
Rc::strong_count(&branch),
|
|
|
Rc::weak_count(&branch),
|
|
|
);
|
|
|
|
|
|
println!(
|
|
|
"leaf strong = {}, weak = {}",
|
|
|
Rc::strong_count(&leaf),
|
|
|
Rc::weak_count(&leaf),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
|
|
|
println!(
|
|
|
"leaf strong = {}, weak = {}",
|
|
|
Rc::strong_count(&leaf),
|
|
|
Rc::weak_count(&leaf),
|
|
|
);
|
|
|
}</code></pre>
|
|
|
<p><span class="caption">示例 15-29:在内部作用域创建 <code>branch</code> 并检查其强弱引用计数</span></p>
|
|
|
<p>一旦创建了 <code>leaf</code>,其 <code>Rc<Node></code> 的强引用计数为 1,弱引用计数为 0。在内部作用域中创建了 <code>branch</code> 并与 <code>leaf</code> 相关联,此时 <code>branch</code> 中 <code>Rc<Node></code> 的强引用计数为 1,弱引用计数为 1(因为 <code>leaf.parent</code> 通过 <code>Weak<Node></code> 指向 <code>branch</code>)。这里 <code>leaf</code> 的强引用计数为 2,因为现在 <code>branch</code> 的 <code>branch.children</code> 中储存了 <code>leaf</code> 的 <code>Rc<Node></code> 的拷贝,不过弱引用计数仍然为 0。</p>
|
|
|
<p>当内部作用域结束时,<code>branch</code> 离开作用域,<code>Rc<Node></code> 的强引用计数减少为 0,所以其 <code>Node</code> 被丢弃。来自 <code>leaf.parent</code> 的弱引用计数 1 与 <code>Node</code> 是否被丢弃无关,所以并没有产生任何内存泄漏!</p>
|
|
|
<p>如果在内部作用域结束后尝试访问 <code>leaf</code> 的父节点,会再次得到 <code>None</code>。在程序的结尾,<code>leaf</code> 中 <code>Rc<Node></code> 的强引用计数为 1,弱引用计数为 0,因为现在 <code>leaf</code> 又是 <code>Rc<Node></code> 唯一的引用了。</p>
|
|
|
<p>所有这些管理计数和值的逻辑都内建于 <code>Rc<T></code> 和 <code>Weak<T></code> 以及它们的 <code>Drop</code> trait 实现中。通过在 <code>Node</code> 定义中指定从子节点到父节点的关系为一个 <code>Weak<T></code> 引用,就能够拥有父节点和子节点之间的双向引用而不会造成引用循环和内存泄漏。</p>
|
|
|
<h2 id="总结"><a class="header" href="#总结">总结</a></h2>
|
|
|
<p>这一章涵盖了如何使用智能指针来做出不同于 Rust 常规引用默认所提供的保证与取舍。<code>Box<T></code> 有一个已知的大小并指向分配在堆上的数据。<code>Rc<T></code> 记录了堆上数据的引用计数从而允许多个所有者。<code>RefCell<T></code> 类型及其内部可变性允许我们在保持类型不可变的前提下更改其内部值;它也在运行时而非编译时执行借用规则检查。</p>
|
|
|
<p>我们还讨论了 trait <code>Deref</code> 和 <code>Drop</code>,它们实现了智能指针的许多功能。同时探索了会造成内存泄漏的引用循环,以及如何使用 <code>Weak<T></code> 来避免它们。</p>
|
|
|
<p>如果本章内容引起了你的兴趣并希望现在就实现你自己的智能指针的话,请阅读 <a href="https://doc.rust-lang.org/nomicon/index.html">“The Rustonomicon”</a> 来获取更多有用的信息。</p>
|
|
|
<p>接下来,让我们谈谈 Rust 的并发。届时甚至还会学习到一些新的对并发有帮助的智能指针。</p>
|
|
|
|
|
|
</main>
|
|
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
|
<!-- Mobile navigation buttons -->
|
|
|
<a rel="prev" href="ch15-05-interior-mutability.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
<a rel="next prefetch" href="ch16-00-concurrency.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
<div style="clear: both"></div>
|
|
|
</nav>
|
|
|
</div>
|
|
|
</div>
|
|
|
|
|
|
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
|
|
<a rel="prev" href="ch15-05-interior-mutability.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z"/></svg></span>
|
|
|
</a>
|
|
|
|
|
|
<a rel="next prefetch" href="ch16-00-concurrency.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
|
|
|
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
|
|
|
</a>
|
|
|
</nav>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<template id=fa-eye><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M288 32c-80.8 0-145.5 36.8-192.6 80.6C48.6 156 17.3 208 2.5 243.7c-3.3 7.9-3.3 16.7 0 24.6C17.3 304 48.6 356 95.4 399.4C142.5 443.2 207.2 480 288 480s145.5-36.8 192.6-80.6c46.8-43.5 78.1-95.4 93-131.1c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C433.5 68.8 368.8 32 288 32zM432 256c0 79.5-64.5 144-144 144s-144-64.5-144-144s64.5-144 144-144s144 64.5 144 144zM288 192c0 35.3-28.7 64-64 64c-11.5 0-22.3-3-31.6-8.4c-.2 2.8-.4 5.5-.4 8.4c0 53 43 96 96 96s96-43 96-96s-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6z"/></svg></span></template>
|
|
|
<template id=fa-eye-slash><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M38.8 5.1C28.4-3.1 13.3-1.2 5.1 9.2S-1.2 34.7 9.2 42.9l592 464c10.4 8.2 25.5 6.3 33.7-4.1s6.3-25.5-4.1-33.7L525.6 386.7c39.6-40.6 66.4-86.1 79.9-118.4c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C465.5 68.8 400.8 32 320 32c-68.2 0-125 26.3-169.3 60.8L38.8 5.1zM223.1 149.5C248.6 126.2 282.7 112 320 112c79.5 0 144 64.5 144 144c0 24.9-6.3 48.3-17.4 68.7L408 294.5c5.2-11.8 8-24.8 8-38.5c0-53-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6c0 10.2-2.4 19.8-6.6 28.3l-90.3-70.8zm223.1 298L373 389.9c-16.4 6.5-34.3 10.1-53 10.1c-79.5 0-144-64.5-144-144c0-6.9 .5-13.6 1.4-20.2L83.1 161.5C60.3 191.2 44 220.8 34.5 243.7c-3.3 7.9-3.3 16.7 0 24.6c14.9 35.7 46.2 87.7 93 131.1C174.5 443.2 239.2 480 320 480c47.8 0 89.9-12.9 126.2-32.5z"/></svg></span></template>
|
|
|
<template id=fa-copy><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M502.6 70.63l-61.25-61.25C435.4 3.371 427.2 0 418.7 0H255.1c-35.35 0-64 28.66-64 64l.0195 256C192 355.4 220.7 384 256 384h192c35.2 0 64-28.8 64-64V93.25C512 84.77 508.6 76.63 502.6 70.63zM464 320c0 8.836-7.164 16-16 16H255.1c-8.838 0-16-7.164-16-16L239.1 64.13c0-8.836 7.164-16 16-16h128L384 96c0 17.67 14.33 32 32 32h47.1V320zM272 448c0 8.836-7.164 16-16 16H63.1c-8.838 0-16-7.164-16-16L47.98 192.1c0-8.836 7.164-16 16-16H160V128H63.99c-35.35 0-64 28.65-64 64l.0098 256C.002 483.3 28.66 512 64 512h192c35.2 0 64-28.8 64-64v-32h-47.1L272 448z"/></svg></span></template>
|
|
|
<template id=fa-play><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg></span></template>
|
|
|
<template id=fa-clock-rotate-left><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M75 75L41 41C25.9 25.9 0 36.6 0 57.9V168c0 13.3 10.7 24 24 24H134.1c21.4 0 32.1-25.9 17-41l-30.8-30.8C155 85.5 203 64 256 64c106 0 192 86 192 192s-86 192-192 192c-40.8 0-78.6-12.7-109.7-34.4c-14.5-10.1-34.4-6.6-44.6 7.9s-6.6 34.4 7.9 44.6C151.2 495 201.7 512 256 512c141.4 0 256-114.6 256-256S397.4 0 256 0C185.3 0 121.3 28.7 75 75zm181 53c-13.3 0-24 10.7-24 24V256c0 6.4 2.5 12.5 7 17l72 72c9.4 9.4 24.6 9.4 33.9 0s9.4-24.6 0-33.9l-65-65V152c0-13.3-10.7-24-24-24z"/></svg></span></template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
window.playground_copyable = true;
|
|
|
</script>
|
|
|
|
|
|
|
|
|
<script src="elasticlunr-ef4e11c1.min.js"></script>
|
|
|
<script src="mark-09e88c2c.min.js"></script>
|
|
|
<script src="searcher-c2a407aa.js"></script>
|
|
|
|
|
|
<script src="clipboard-1626706a.min.js"></script>
|
|
|
<script src="highlight-abc7f01d.js"></script>
|
|
|
<script src="book-a0b12cfe.js"></script>
|
|
|
|
|
|
<!-- Custom JS scripts -->
|
|
|
<script src="ferris-2317480c.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
</body>
|
|
|
</html>
|