2017-02-11 14:49:51 +08:00
<!DOCTYPE HTML>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
2017-02-26 15:11:54 +08:00
< title > 变量和可变性 - Rust 程序设计语言 简体中文版< / title >
2017-02-11 14:49:51 +08:00
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
2017-02-26 15:11:54 +08:00
< meta name = "description" content = "Rust 程序设计语言 简体中文版" >
2017-02-11 14:49:51 +08:00
< 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-05 23:13:49 +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" class = "active" > < 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-
2017-02-11 14:49:51 +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 >
2017-02-26 15:11:54 +08:00
< h1 class = "menu-title" > Rust 程序设计语言 简体中文版< / h1 >
2017-02-11 14:49:51 +08:00
< div class = "right-buttons" >
< i id = "print-button" class = "fa fa-print" title = "Print this book" > < / i >
< / div >
< / div >
< div id = "content" class = "content" >
2017-02-26 15:11:54 +08:00
< a class = "header" href = "#变量和可变性" name = "变量和可变性" > < h2 > 变量和可变性< / h2 > < / a >
2017-02-11 14:49:51 +08:00
< blockquote >
2017-03-19 14:53:41 +08:00
< p > < a href = "https://github.com/rust-lang/book/blob/master/second-edition/src/ch03-01-variables-and-mutability.md" > ch03-01-variables-and-mutability.md< / a >
2017-02-11 14:49:51 +08:00
< br >
2017-03-19 14:53:41 +08:00
commit 04aa3a45eb72855b34213703718f50a12a3eeec8< / p >
2017-02-11 14:49:51 +08:00
< / blockquote >
2017-03-19 14:53:41 +08:00
< p > 第二章中提到过,变量默认是< strong > 不可变< / strong > ( < em > immutable< / em > )的。这是 Rust 中许多鼓励利用 Rust 提供的安全和简单并发优势编写代码的助力之一。不过,仍然有使变量可变的选项。让我们探索一下如何以及为什么鼓励你拥抱不可变性,还有为什么你可能想要弃之不用。< / p >
< p > 当变量是不可变时,这意味着一旦一个值被绑定上了一个名称,你就不能改变这个值。作为说明,通过< code > cargo new --bin variables< / code > 在 < em > projects< / em > 目录生成一个叫做 < em > variables< / em > 的新项目。< / p >
2017-02-11 14:49:51 +08:00
< p > 接着,在新建的 < em > variables< / em > 目录,打开 < em > src/main.rs< / em > 并替换其代码为如下:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust,ignore" > fn main() {
let x = 5;
println!(" The value of x is: {}" , x);
x = 6;
println!(" The value of x is: {}" , x);
}
< / code > < / pre >
< p > 保存并使用< code > cargo run< / code > 运行程序。应该会看到一个错误信息,如下输出所示:< / p >
2017-03-19 14:53:41 +08:00
< pre > < code > $ cargo run
2017-02-11 14:49:51 +08:00
Compiling variables v0.0.1 (file:///projects/variables)
error[E0384]: re-assignment of immutable variable `x`
--> src/main.rs:4:5
|
2 | let x = 5;
| - first assignment to `x`
3 | println!(" The value of x is: {}" , x);
4 | x = 6;
| ^^^^^ re-assignment of immutable variable
< / code > < / pre >
2017-02-14 22:42:54 +08:00
< p > 这个例子显示了编译器如何帮助你寻找程序中的错误。即便编译器错误可能是令人沮丧的,他们也仅仅意味着程序不能安全的完成你想让它完成的工作;他们< strong > 不能< / strong > 说明你不是一个好的程序员!有经验的 Rustacean 们也会遇到编译器错误。这些错误表明错误的原因是< code > 对不可变变量重新赋值< / code > ( < code > re-assignment of immutable variable< / code > ),因为我们尝试对不可变变量< code > x< / code > 赋第二个值。< / p >
2017-03-19 14:53:41 +08:00
< p > 当尝试去改变之前设计为不可变的值出现编译时错误是很重要的,因为这种情况可能导致 bug。如果代码的一部分假设一个值永远也不会改变而另一部分代码改变了它, 这样第一部分代码就有可能不能像它设计的那样运行。不得不承认这种 bug 难以跟踪,尤其是当第二部分代码只是< strong > 有时< / strong > 当变量使不可变时。< / p >
2017-02-11 14:49:51 +08:00
< p > Rust 编译器保证如果声明一个值不会改变,它就真的不会改变。这意味着当阅读和编写代码时,并不需要记录如何以及在哪可能会被改变,这使得代码易于推导。< / p >
< p > 不过可变性也是非常有用的。变量只是默认不可变;可以通过在变量名之前增加< code > mut< / code > 来使其可变。它向之后的读者表明了其他部分的代码将会改变这个变量值的意图。< / p >
< p > 例如,改变 < em > src/main.rs< / em > 并替换其代码为如下:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > fn main() {
let mut x = 5;
println!(" The value of x is: {}" , x);
x = 6;
println!(" The value of x is: {}" , x);
}
< / code > < / pre >
< p > 当运行这个程序,出现如下:< / p >
2017-03-19 14:53:41 +08:00
< pre > < code > $ cargo run
2017-02-11 14:49:51 +08:00
Compiling variables v0.1.0 (file:///projects/variables)
Running `target/debug/variables`
The value of x is: 5
The value of x is: 6
< / code > < / pre >
2017-03-19 14:53:41 +08:00
< p > 通过< code > mut< / code > ,允许把绑定到< code > x< / code > 的值从< code > 5< / code > 改成< code > 6< / code > 。在一些情况下,你会想要一个变量是可变的,因为这比只使用不可变变量实现的代码更易于编写。< / p >
< p > 除了避免 bug 外,这里还有多个需要权衡取舍的地方。例如,有时使用大型数据结构时,适当地使变量可变可能比复制和返回新分配的实例要更快。对于较小的数据结构,总是创建新实例并采用一种更函数式的编程风格可能会使代码更易理解。所以为了可读性而造成的性能惩罚也许是值得的。< / p >
2017-02-26 15:11:54 +08:00
< a class = "header" href = "#变量和常量的区别" name = "变量和常量的区别" > < h3 > 变量和常量的区别< / h3 > < / a >
2017-03-19 14:53:41 +08:00
< p > 不能改变一个变量的值可能会使你想起另一个大部分编程语言都有的概念:< strong > 常量< / strong > ( < em > constants< / em > )。类似于不可变变量,常量也是绑定到一个名称的不允许改变的值,不过常量与变量还是有一些区别。< / p >
< p > 首先,不允许对常量使用< code > mut< / code > :常量不光是默认不能改变,它总是不能改变。< / p >
< p > 常量使用< code > const< / code > 关键字而不是< code > let< / code > 关键字声明,而且< em > 必须< / em > 注明值的类型。现在我们准备在下一部分,“数据类型”,涉及到类型和类型注解,所以现在无需担心这些细节,只需记住必须总是注明类型即可。< / p >
< p > 常量可以在任何作用域声明,包括全局作用域,这在一个值需要被很多部分的代码用到时很有用。< / p >
< p > 最后一个区别是常量只能用于常量表达式,而不能作为函数调用的结果或任何其他只在运行时计算的值。< / p >
< p > 这是一个常量声明的例子,它的名称是< code > MAX_POINTS< / code > 而它的值是 100,000。( Rust 常量的命名规范是使用大写字母和单词间使用下划线):< / p >
2017-02-11 14:49:51 +08:00
< pre > < code class = "language-rust" > const MAX_POINTS: u32 = 100_000;
< / code > < / pre >
2017-03-19 14:53:41 +08:00
< p > 常量在整个程序生命周期中都有效,位于它声明的作用域之中。这使得常量可以用作多个部分的代码可能需要知道的程序范围的值,例如一个游戏中任何玩家可以获得的最高分或者光速。< / p >
< p > 将用于整个程序的硬编码的值声明为为常量(并编写文档)对为将来代码维护者表明值的意义是很有用的。它也能帮助你将硬编码的值至于一处以便将来可能需要修改他们。< / p >
< a class = "header" href = "#覆盖shadowing" name = "覆盖shadowing" > < h3 > 覆盖( Shadowing) < / h3 > < / a >
2017-02-14 22:42:54 +08:00
< p > 如第二章猜猜看游戏所讲到的,我们可以定义一个与之前变量名称相同的新变量,而新变量会< strong > 覆盖< / strong > 之前的变量。Rustacean 们称其为第一个变量被第二个< strong > 给覆盖< / strong > 了,这意味着第二个变量的值是使用这个变量时会看到的值。可以用相同变量名称来覆盖它自己以及重复使用< code > let< / code > 关键字来多次覆盖,如下所示:< / p >
2017-02-11 14:49:51 +08:00
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > fn main() {
let x = 5;
let x = x + 1;
let x = x * 2;
println!(" The value of x is: {}" , x);
}
< / code > < / pre >
< p > 这个程序首先将< code > x< / code > 绑定到值< code > 5< / code > 上。接着通过< code > let x =< / code > 覆盖< code > x< / code > ,获取原始值并加< code > 1< / code > 这样< code > x< / code > 的值就变成< code > 6< / code > 了。第三个< code > let< / code > 语句也覆盖了< code > x< / code > ,获取之前的值并乘以< code > 2< / code > , < code > x< / code > 的最终值是< code > 12< / code > 。当运行这个程序,它会有如下输出:< / p >
2017-03-19 14:53:41 +08:00
< pre > < code > $ cargo run
2017-02-11 14:49:51 +08:00
Compiling variables v0.1.0 (file:///projects/variables)
Running `target/debug/variables`
The value of x is: 12
< / code > < / pre >
< p > 这与将变量声明为< code > mut< / code > 是有区别的。因为除非再次使用< code > let< / code > 关键字,不小心尝试对变量重新赋值会导致编译时错误。我们可以用这个值进行一些计算,不过计算完之后变量仍然是不变的。< / p >
< p > 另一个< code > mut< / code > 与覆盖的区别是当再次使用< code > let< / code > 关键字时,事实上创建了一个新变量,我们可以改变值的类型。例如,假设程序请求用户输入空格来提供在一些文本之间需要多少空间来分隔,不过我们真正需要的是将输入存储成数字(多少个空格):< / p >
< pre > < code class = "language-rust" > let spaces = " " ;
let spaces = spaces.len();
< / code > < / pre >
< p > 这里允许第一个< code > spaces< / code > 变量是字符串类型,而第二个< code > spaces< / code > 变量,它是一个恰巧与第一个变量名字相同的崭新的变量,它是数字类型。因此覆盖使我们不必使用不同的名字,比如< code > spaces_str< / code > 和< code > spaces_num< / code > ;相反,我们可以复用< code > spaces< / code > 这个更简单的名称。然而,如果尝试使用< code > mut< / code > ,如下所示:< / p >
< pre > < code class = "language-rust,ignore" > let mut spaces = " " ;
spaces = spaces.len();
< / code > < / pre >
< p > 会导致一个编译时错误,因为不允许改变一个变量的类型:< / p >
2017-03-19 14:53:41 +08:00
< pre > < code > error[E0308]: mismatched types
2017-02-11 14:49:51 +08:00
--> src/main.rs:3:14
|
3 | spaces = spaces.len();
| ^^^^^^^^^^^^ expected & str, found usize
|
= note: expected type `& str`
= note: found type `usize`
< / code > < / pre >
2017-03-19 14:53:41 +08:00
< p > 现在我们探索了变量如何工作,让我们看看更多的数据类型。< / p >
2017-02-11 14:49:51 +08:00
< / div >
<!-- Mobile navigation buttons -->
< a href = "ch03-00-common-programming-concepts.html" class = "mobile-nav-chapters previous" >
< i class = "fa fa-angle-left" > < / i >
< / a >
< a href = "ch03-02-data-types.html" class = "mobile-nav-chapters next" >
< i class = "fa fa-angle-right" > < / i >
< / a >
< / div >
< a href = "ch03-00-common-programming-concepts.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 = "ch03-02-data-types.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 >