2017-03-13 23:27:01 +08:00
<!DOCTYPE HTML>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title > `Box< T> `用于已知大小的堆上数据 - Rust 程序设计语言 简体中文版< / title >
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
< meta name = "description" content = "Rust 程序设计语言 简体中文版" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< base href = "" >
< link rel = "stylesheet" href = "book.css" >
< link href = 'https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel = 'stylesheet' type = 'text/css' >
< link rel = "shortcut icon" href = "favicon.png" >
<!-- Font Awesome -->
< link rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" >
< link rel = "stylesheet" href = "highlight.css" >
< link rel = "stylesheet" href = "tomorrow-night.css" >
<!-- MathJax -->
< script type = "text/javascript" src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" > < / script >
<!-- Fetch JQuery from CDN but have a local fallback -->
< script src = "https://code.jquery.com/jquery-2.1.4.min.js" > < / script >
< script >
if (typeof jQuery == 'undefined') {
document.write(unescape("%3Cscript src='jquery.js'%3E%3C/script%3E"));
}
< / script >
< / head >
< body class = "light" >
<!-- Set the theme before any content is loaded, prevents flash -->
< script type = "text/javascript" >
var theme = localStorage.getItem('theme');
if (theme == null) { theme = 'light'; }
$('body').removeClass().addClass(theme);
< / script >
<!-- Hide / unhide sidebar before it is displayed -->
< script type = "text/javascript" >
var sidebar = localStorage.getItem('sidebar');
if (sidebar === "hidden") { $("html").addClass("sidebar-hidden") }
else if (sidebar === "visible") { $("html").addClass("sidebar-visible") }
< / script >
< div id = "sidebar" class = "sidebar" >
2017-04-18 14:55:09 +08:00
< ul class = "chapter" > < li > < a href = "ch01-00-introduction.html" > < strong > 1.< / strong > 介绍< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch01-01-installation.html" > < strong > 1.1.< / strong > 安装< / a > < / li > < li > < a href = "ch01-02-hello-world.html" > < strong > 1.2.< / strong > Hello, World!< / a > < / li > < / ul > < / li > < li > < a href = "ch02-00-guessing-game-tutorial.html" > < strong > 2.< / strong > 猜猜看教程< / a > < / li > < li > < a href = "ch03-00-common-programming-concepts.html" > < strong > 3.< / strong > 通用编程概念< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch03-01-variables-and-mutability.html" > < strong > 3.1.< / strong > 变量和可变性< / a > < / li > < li > < a href = "ch03-02-data-types.html" > < strong > 3.2.< / strong > 数据类型< / a > < / li > < li > < a href = "ch03-03-how-functions-work.html" > < strong > 3.3.< / strong > 函数如何工作< / a > < / li > < li > < a href = "ch03-04-comments.html" > < strong > 3.4.< / strong > 注释< / a > < / li > < li > < a href = "ch03-05-control-flow.html" > < strong > 3.5.< / strong > 控制流< / a > < / li > < / ul > < / li > < li > < a href = "ch04-00-understanding-ownership.html" > < strong > 4.< / strong > 认识所有权< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch04-01-what-is-ownership.html" > < strong > 4.1.< / strong > 什么是所有权< / a > < / li > < li > < a href = "ch04-02-references-and-borrowing.html" > < strong > 4.2.< / strong > 引用 & 借用< / a > < / li > < li > < a href = "ch04-03-slices.html" > < strong > 4.3.< / strong > Slices< / a > < / li > < / ul > < / li > < li > < a href = "ch05-00-structs.html" > < strong > 5.< / strong > 结构体< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch05-01-method-syntax.html" > < strong > 5.1.< / strong > 方法语法< / a > < / li > < / ul > < / li > < li > < a href = "ch06-00-enums.html" > < strong > 6.< / strong > 枚举和模式匹配< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch06-01-defining-an-enum.html" > < strong > 6.1.< / strong > 定义枚举< / a > < / li > < li > < a href = "ch06-02-match.html" > < strong > 6.2.< / strong > < code > match< / code > 控制流运算符< / a > < / li > < li > < a href = "ch06-03-if-let.html" > < strong > 6.3.< / strong > < code > if let< / code > 简单控制流< / a > < / li > < / ul > < / li > < li > < a href = "ch07-00-modules.html" > < strong > 7.< / strong > 模块< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch07-01-mod-and-the-filesystem.html" > < strong > 7.1.< / strong > < code > mod< / code > 和文件系统< / a > < / li > < li > < a href = "ch07-02-controlling-visibility-with-pub.html" > < strong > 7.2.< / strong > 使用< code > pub< / code > 控制可见性< / a > < / li > < li > < a href = "ch07-03-importing-names-with-use.html" > < strong > 7.3.< / strong > 使用< code > use< / code > 导入命名< / a > < / li > < / ul > < / li > < li > < a href = "ch08-00-common-collections.html" > < strong > 8.< / strong > 通用集合类型< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch08-01-vectors.html" > < strong > 8.1.< / strong > vector< / a > < / li > < li > < a href = "ch08-02-strings.html" > < strong > 8.2.< / strong > 字符串< / a > < / li > < li > < a href = "ch08-03-hash-maps.html" > < strong > 8.3.< / strong > 哈希 map< / a > < / li > < / ul > < / li > < li > < a href = "ch09-00-error-handling.html" > < strong > 9.< / strong > 错误处理< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch09-01-unrecoverable-errors-with-panic.html" > < strong > 9.1.< / strong > < code > panic!< / code > 与不可恢复的错误< / a > < / li > < li > < a href = "ch09-02-recoverable-errors-with-result.html" > < strong > 9.2.< / strong > < code > Result< / code > 与可恢复的错误< / a > < / li > < li > < a href = "ch09-03-to-panic-or-not-to-panic.html" > < strong > 9.3.< / strong > < code > panic!< / code > 还是不< code > panic!< / code > < / a > < / li > < / ul > < / li > < li > < a href = "ch10-00-generics.html" > < strong > 10.< / strong > 泛型、trait 和生命周期< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch10-01-syntax.html" > < strong > 10.1.< / strong > 泛型数据类型< / a > < / li > < li > < a href = "ch10-02-traits.html" > < strong > 10.2.< / strong > trait: 定义共享的行为< / a > < / li > < li > < a href = "ch10-03-lifetime-syntax.html" > < strong > 10.3.< / strong > 生命周期与引用有效性< / a > < / li > < / ul > < / li > < li > < a href = "ch11-00-testing.html" > < strong > 11.< / strong > 测试< / a > < / li > < li > < ul class = "section" > < li > < a href = "ch11-01-writing-tests.html" > < strong > 11.1.< / strong > 编写测试< / a > < / li > < li > < a href = "ch11-02-running-tests.html" > < strong > 11.2.< / strong > 运行测试< / a > < / li > < li > < a href = "ch11-03-test-organization.html" > < strong > 11.3.< / strong > 测试的组织结构< / a > < / li > < / ul > < / li > < li > < a href = "ch12-00-an-io-project.h
2017-03-13 23:27:01 +08:00
< / div >
< div id = "page-wrapper" class = "page-wrapper" >
< div class = "page" >
< div id = "menu-bar" class = "menu-bar" >
< div class = "left-buttons" >
< i id = "sidebar-toggle" class = "fa fa-bars" > < / i >
< i id = "theme-toggle" class = "fa fa-paint-brush" > < / i >
< / div >
< h1 class = "menu-title" > Rust 程序设计语言 简体中文版< / h1 >
< div class = "right-buttons" >
< i id = "print-button" class = "fa fa-print" title = "Print this book" > < / i >
< / div >
< / div >
< div id = "content" class = "content" >
< a class = "header" href = "#boxt用于已知大小的堆上数据" name = "boxt用于已知大小的堆上数据" > < h2 > < code > Box< T> < / code > 用于已知大小的堆上数据< / h2 > < / a >
< blockquote >
< p > < a href = "https://github.com/rust-lang/book/blob/master/second-edition/src/ch15-01-box.md" > ch15-01-box.md< / a >
< br >
commit 85b2c9ac704c9dc4bbedb97209d336afb9809dc1< / p >
< / blockquote >
< p > 最简单直接的智能指针是 < em > box< / em > ,它的类型是< code > Box< T> < / code > 。 box 允许你将一个单独的值放在堆上(第四章介绍或栈与堆)。列表 15-1 展示了如何使用 box 在堆上储存一个< code > i32< / code > : < / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > fn main() {
let b = Box::new(5);
println!(" b = {}" , b);
}
< / code > < / pre >
< p > < span class = "caption" > Listing 15-1: Storing an < code > i32< / code > value on the heap using a
box< / span > < / p >
< p > 这会打印出< code > b = 5< / code > 。在这个例子中,我们可以像数据是储存在栈上的那样访问 box 中的数据。正如任何拥有数据所有权的值那样,当像< code > b< / code > 这样的 box 在< code > main< / code > 的末尾离开作用域时,它将被释放。这个释放过程作用于 box 本身(位于栈上)和它所指向的数据(位于堆上)。< / p >
< p > 将一个单独的值存放在堆上并不是很有意义,所以像列表 15-1 这样单独使用 box 并不常见。一个 box 的实用场景是当你希望确保类型有一个已知大小的时候。例如,考虑一下列表 15-2, 它是一个用于 < em > cons list< / em > 的枚举定义,这是一个来源于函数式编程的数据结构类型。注意它还不能编译:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust,ignore" > enum List {
Cons(i32, List),
Nil,
}
< / code > < / pre >
< p > < span class = "caption" > Listing 15-2: The first attempt of defining an enum to
represent a cons list data structure of < code > i32< / code > values< / span > < / p >
< p > 我们实现了一个只存放< code > i32< / code > 值的 cons list。也可以选择实用第十章介绍的泛型来实现一个类型无关的 cons list。< / p >
< blockquote >
< a class = "header" href = "#cons-list-的更多内容" name = "cons-list-的更多内容" > < h4 > cons list 的更多内容< / h4 > < / a >
< p > < em > cons list< / em > 是一个来源于 Lisp 编程语言及其方言的数据结构。在 Lisp 中,< code > cons< / code > 函数(" construct function" 的缩写)利用两个参数来构造一个新的列表,他们通常是一个单独的值和另一个列表。< / p >
< p > cons 函数的概念涉及到更通用的函数式编程术语;“将 x 与 y 连接”通常意味着构建一个新的容器而将 x 的元素放在新容器的开头,其后则是容器 y 的元素。< / p >
< p > cons list 通过递归调用< code > cons< / code > 函数产生。代表递归的 base case 的规范名称是< code > Nil< / code > ,它宣布列表的终止。注意这不同于第六章中的" null" 或" nil" 的概念,他们代表无效或缺失的值。< / p >
< / blockquote >
< p > cons list 是一个每个元素和之后的其余部分都只包含一个值的列表。列表的其余部分由嵌套的 cons list 定义。其结尾由值< code > Nil< / code > 表示。cons list 在 Rust 中并不常见;通常< code > Vec< T> < / code > 是一个更好的选择。实现这个数据结构是< code > Box< T> < / code > 实用性的一个好的例子。让我们看看为什么!< / p >
< p > 使用 cons list 来储存列表< code > 1, 2, 3< / code > 将看起来像这样:< / p >
< pre > < code class = "language-rust,ignore" > use List::{Cons, Nil};
fn main() {
let list = Cons(1, Cons(2, Cons(3, Nil)));
}
< / code > < / pre >
< p > 第一个< code > Cons< / code > 储存了< code > 1< / code > 和另一个< code > List< / code > 值。这个< code > List< / code > 是另一个包含< code > 2< / code > 的< code > Cons< / code > 值和下一个< code > List< / code > 值。这又是另一个存放了< code > 3< / code > 的< code > Cons< / code > 值和最后一个值为< code > Nil< / code > 的< code > List< / code > ,非递归成员代表了列表的结尾。< / p >
< p > 如果尝试编译上面的代码,会得到如列表 15-3 所示的错误:< / p >
< pre > < code > error[E0072]: recursive type `List` has infinite size
-->
|
1 | enum List {
| _^ starting here...
2 | | Cons(i32, List),
3 | | Nil,
4 | | }
| |_^ ...ending here: recursive type has infinite size
|
= help: insert indirection (e.g., a `Box`, `Rc`, or `& `) at some point to
make `List` representable
< / code > < / pre >
< p > < span class = "caption" > Listing 15-3: The error we get when attempting to define
a recursive enum< / span > < / p >
< p > 错误表明这个类型“有无限的大小”。为什么呢?因为< code > List< / code > 的一个成员被定义为递归的:它存放了另一个相同类型的值。这意味着 Rust 无法计算为了存放< code > List< / code > 值到底需要多少空间。让我们一点一点的看:首先了解一下 Rust 如何决定需要多少空间来存放一个非递归类型。回忆一下第六章讨论枚举定义时的列表 6-2 中定义的< code > Message< / code > 枚举:< / p >
< pre > < code class = "language-rust" > enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
< / code > < / pre >
< p > 当 Rust 需要知道需要为< code > Message< / code > 值分配多少空间时,它可以检查每一个成员并发现< code > Message::Quit< / code > 并不需要任何空间,< code > Message::Move< / code > 需要足够储存两个< code > i32< / code > 值的空间,依此类推。因此,< code > Message< / code > 值所需的最大空间等于储存其最大成员的空间大小。< / p >
< p > 与此相对当 Rust 编译器检查像列表 15-2 中的< code > List< / code > 这样的递归类型时会发生什么呢。编译器尝试计算出储存一个< code > List< / code > 枚举需要多少内存,并开始检查< code > Cons< / code > 成员,那么< code > Cons< / code > 需要的空间等于< code > i32< / code > 的大小加上< code > List< / code > 的大小。为了计算< code > List< / code > 需要多少内存,它检查其成员,从< code > Cons< / code > 成员开始。< code > Cons< / code > 成员储存了一个< code > i32< / code > 值和一个< code > List< / code > 值,这样的计算将无限进行下去,如图 15-4 所示:< / p >
< p > < img alt = "An infinite Cons list" src = "img/trpl15-01.svg" class = "center" style = "width: 50%;" / > < / p >
< p > < span class = "caption" > Figure 15-4: An infinite < code > List< / code > consisting of infinite
< code > Cons< / code > variants< / span > < / p >
< p > Rust 无法计算出要为定义为递归的类型分配多少空间,所以编译器给出了列表 15-3 中的错误。这个错误也包括了有用的建议:< / p >
< pre > < code class = "language-text" > = help: insert indirection (e.g., a `Box`, `Rc`, or `& `) at some point to
make `List` representable
< / code > < / pre >
< p > 因为< code > Box< T> < / code > 是一个指针,我们总是知道它需要多少空间:指针需要一个< code > usize< / code > 大小的空间。这个< code > usize< / code > 的值将是堆数据的地址。而堆数据可以是任意大小,不过开始这个堆数据的地址总是能放进一个< code > usize< / code > 中。所以如果将列表 15-2 的定义修改为像这里列表 15-5 中的定义,并修改< code > main< / code > 函数为< code > Cons< / code > 成员中的值使用< code > Box::new< / code > : < / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > enum List {
Cons(i32, Box< List> ),
Nil,
}
use List::{Cons, Nil};
fn main() {
let list = Cons(1,
Box::new(Cons(2,
Box::new(Cons(3,
Box::new(Nil))))));
}
< / code > < / pre >
< p > < span class = "caption" > Listing 15-5: Definition of < code > List< / code > that uses < code > Box< T> < / code > in
order to have a known size< / span > < / p >
< p > 这样编译器就能够计算出储存一个< code > List< / code > 值需要的大小了。Rust 将会检查< code > List< / code > ,同样的从< code > Cons< / code > 成员开始检查。< code > Cons< / code > 成员需要< code > i32< / code > 的大小加上一个< code > usize< / code > 的大小,因为 box 总是< code > usize< / code > 大小的,不管它指向的是什么。接着 Rust 检查< code > Nil< / code > 成员,它并储存一个值,所以< code > Nil< / code > 并不需要任何空间。我们通过 box 打破了这无限递归的连锁。图 15-6 展示了现在< code > Cons< / code > 成员看起来像什么:< / p >
< p > < img alt = "A finite Cons list" src = "img/trpl15-02.svg" class = "center" / > < / p >
< p > < span class = "caption" > Figure 15-6: A < code > List< / code > that is not infinitely sized since
< code > Cons< / code > holds a < code > Box< / code > < / span > < / p >
< p > 这就是 box 主要应用场景:打破无限循环的数据结构以便编译器可以知道其大小。第十七章讨论 trait 对象时我们将了解另一个 Rust 中会出现未知大小数据的情况。< / p >
< p > 虽然我们并不经常使用 box, 他们也是一个了解智能指针模式的好的方式。< code > Box< T> < / code > 作为智能指针经常被使用的两个方面是他们< code > Deref< / code > 和< code > Drop< / code > trait 的实现。让我们研究这些 trait 如何工作以及智能指针如何利用他们。< / p >
< / div >
<!-- Mobile navigation buttons -->
< a href = "ch15-00-smart-pointers.html" class = "mobile-nav-chapters previous" >
< i class = "fa fa-angle-left" > < / i >
< / a >
< a href = "ch15-02-deref.html" class = "mobile-nav-chapters next" >
< i class = "fa fa-angle-right" > < / i >
< / a >
< / div >
< a href = "ch15-00-smart-pointers.html" class = "nav-chapters previous" title = "You can navigate through the chapters using the arrow keys" >
< i class = "fa fa-angle-left" > < / i >
< / a >
< a href = "ch15-02-deref.html" class = "nav-chapters next" title = "You can navigate through the chapters using the arrow keys" >
< i class = "fa fa-angle-right" > < / i >
< / a >
< / div >
<!-- Local fallback for Font Awesome -->
< script >
if ($(".fa").css("font-family") !== "FontAwesome") {
$('< link rel = "stylesheet" type = "text/css" href = "_FontAwesome/css/font-awesome.css" > ').prependTo('head');
}
< / script >
<!-- Livereload script (if served using the cli tool) -->
< script src = "highlight.js" > < / script >
< script src = "book.js" > < / script >
< / body >
< / html >