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-02-25 23:47:33 +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" class = "active" > < 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 > < / ul >
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-13 15:43:15 +08:00
< blockquote >
< p > < a href = "https://github.com/rust-lang/book/blob/master/src/ch03-05-control-flow.md" > ch03-05-control-flow.md< / a >
< br >
commit 784a3ec5e8b9c6bff456ab9f0efd4dabcc180dda< / p >
< / blockquote >
< p > 通过条件是不是真来决定是否某些代码, 或者根据条件是否为真来重复运行一段代码是大部分编程语言的基本组成部分。Rust 代码中最常见的用来控制执行流的结构是< code > if< / code > 表达式和循环。< / p >
2017-02-26 15:11:54 +08:00
< a class = "header" href = "#if表达式" name = "if表达式" > < h3 > < code > if< / code > 表达式< / h3 > < / a >
2017-02-13 15:43:15 +08:00
< p > < code > if< / code > 表达式允许根据条件执行不同的代码分支。我们提供一个条件并表示“如果符合这个条件,运行这段代码。如果条件不满足,不运行这段代码。”< / p >
< p > 在 < em > projects< / em > 目录创建一个叫做 < em > branches< / em > 的新项目来学习< code > if< / code > 表达式。在 < em > src/main.rs< / em > 文件中,输入如下内容:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > fn main() {
let number = 3;
if number < 5 {
println!(" condition was true" );
} else {
println!(" condition was false" );
}
}
< / code > < / pre >
< p > 所有< code > if< / code > 表达式以< code > if< / code > 关键字开头,它后跟一个条件。在这个例子中,条件检查< code > number< / code > 是否有一个小于 5 的值。在条件为真时希望执行的代码块位于紧跟条件之后的大括号中。< code > if< / code > 表达式中与条件关联的代码块有时被叫做 < em > arms< / em > ,就像第二章“比较猜测与秘密数字”部分中讨论到的< code > match< / code > 表达式中分支一样。也可以包含一个可选的< code > else< / code > 表达式,这里我们就这么做了,来提供一个在条件为假时应当执行的代码块。如果不提供< code > else< / code > 表达式并且条件为假时,程序会直接忽略< code > if< / code > 代码块并继续执行下面的代码。< / p >
< p > 尝试运行代码,应该能看到如下输出:< / p >
< pre > < code class = "language-sh" > $ cargo run
Compiling branches v0.1.0 (file:///projects/branches)
Running `target/debug/branches`
condition was true
< / code > < / pre >
< p > 尝试改变< code > number< / code > 的值使条件为假时看看会发生什么:< / p >
< pre > < code class = "language-rust,ignore" > let number = 7;
< / code > < / pre >
< p > 再次运行程序并查看输出:< / p >
< pre > < code class = "language-sh" > $ cargo run
Compiling branches v0.1.0 (file:///projects/branches)
Running `target/debug/branches`
condition was false
< / code > < / pre >
2017-02-14 22:42:54 +08:00
< p > 另外值得注意的是代码中的条件< strong > 必须< / strong > 是< code > bool< / code > 。如果像看看条件不是< code > bool< / code > 值时会发生什么,尝试运行如下代码:< / p >
2017-02-13 15:43:15 +08:00
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust,ignore" > fn main() {
let number = 3;
if number {
println!(" number was three" );
}
}
< / code > < / pre >
< p > 这里< code > if< / code > 条件的值是< code > 3< / code > , Rust 抛出了一个错误:< / p >
< pre > < code class = "language-sh" > Compiling branches v0.1.0 (file:///projects/branches)
error[E0308]: mismatched types
--> src/main.rs:4:8
|
4 | if number {
| ^^^^^^ expected bool, found integral variable
|
= note: expected type `bool`
= note: found type `{integer}`
error: aborting due to previous error
Could not compile `branches`.
< / code > < / pre >
< p > 这个错误表明 Rust 期望一个< code > bool< / code > 不过却得到了一个整型。Rust 并不会尝试自动地将非布尔值转换为布尔值,不像例如 Ruby 和 JavaScript 这样的语言。必须总是显式地使用< code > boolean< / code > 作为< code > if< / code > 的条件。例如如果想要< code > if< / code > 代码块只在一个数字不等于< code > 0< / code > 时执行,可以把< code > if< / code > 表达式修改为如下:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > fn main() {
let number = 3;
if number != 0 {
println!(" number was something other than zero" );
}
}
< / code > < / pre >
< p > 运行代码会打印出< code > number was something other than zero< / code > 。< / p >
2017-02-26 15:11:54 +08:00
< a class = "header" href = "#使用else-if实现多重条件" name = "使用else-if实现多重条件" > < h4 > 使用< code > else if< / code > 实现多重条件< / h4 > < / a >
2017-02-13 15:43:15 +08:00
< p > 可以将< code > else if< / code > 表达式与< code > if< / code > 和< code > else< / code > 组合来实现多重条件。例如:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > fn main() {
let number = 6;
if number % 4 == 0 {
println!(" number is divisible by 4" );
} else if number % 3 == 0 {
println!(" number is divisible by 3" );
} else if number % 2 == 0 {
println!(" number is divisible by 2" );
} else {
println!(" number is not divisible by 4, 3, or 2" );
}
}
< / code > < / pre >
< p > 这个程序有四个可能的执行路径。运行后应该能看到如下输出:< / p >
< pre > < code class = "language-sh" > $ cargo run
Compiling branches v0.1.0 (file:///projects/branches)
Running `target/debug/branches`
number is divisible by 3
< / code > < / pre >
< p > 当执行这个程序,它按顺序检查每个< code > if< / code > 表达式并执行第一个条件为真的代码块。注意即使 6 可以被 2 整除,也不会出现< code > number is divisible by 2< / code > 的输出,更不会出现< code > else< / code > 块中的< code > number is not divisible by 4, 3, or 2< / code > 。原因是 Rust 只会执行第一个条件为真的代码块,并且它一旦找到一个以后,就不会检查剩下的条件了。< / p >
< p > 使用过多的< code > else if< / code > 表达式会使代码显得杂乱无章,所以如果有多于一个< code > else if< / code > ,最好重构代码。为此第六章介绍了 Rust 一个叫做< code > match< / code > 的强大的分支结构( branching construct) 。< / p >
2017-02-26 15:11:54 +08:00
< a class = "header" href = "#在let语句中使用if" name = "在let语句中使用if" > < h4 > 在< code > let< / code > 语句中使用< code > if< / code > < / h4 > < / a >
2017-02-13 15:43:15 +08:00
< p > 因为< code > if< / code > 是一个表达式,我们可以在< code > let< / code > 语句的右侧使用它,例如列表 3-4: < / p >
< figure >
< span class = "filename" > Filename: src/main.rs< / span >
< pre > < code class = "language-rust" > fn main() {
let condition = true;
let number = if condition {
5
} else {
6
};
println!(" The value of number is: {}" , number);
}
< / code > < / pre >
< figcaption >
< p > Listing 3-4: Assigning the result of an < code > if< / code > expression to a variable< / p >
< / figcaption >
< / figure >
< p > < code > number< / code > 变量将会绑定到基于< code > if< / code > 表达式结果的值。运行这段代码看看会出现什么:< / p >
< pre > < code class = "language-sh" > $ cargo run
Compiling branches v0.1.0 (file:///projects/branches)
Running `target/debug/branches`
The value of number is: 5
< / code > < / pre >
< p > 还记得代码块的值是其最后一个表达式的值,以及数字本身也是一个表达式吗。在这个例子中,整个< code > if< / code > 表达式的值依赖哪个代码块被执行。这意味着< code > if< / code > 的每个分支的可能的返回值都必须是相同类型;在列表 3-4 中,< code > if< / code > 分支和< code > else< / code > 分支的结果都是< code > i32< / code > 整型。不过如果像下面的例子一样这些类型并不相同会怎么样呢?< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust,ignore" > fn main() {
let condition = true;
let number = if condition {
5
} else {
" six"
};
println!(" The value of number is: {}" , number);
}
< / code > < / pre >
< p > 当运行这段代码,会得到一个错误。< code > if< / code > 和< code > else< / code > 分支的值类型是不相容的,同时 Rust 也准确地表明了在程序中的何处发现的这个问题:< / p >
< pre > < code class = "language-sh" > Compiling branches v0.1.0 (file:///projects/branches)
error[E0308]: if and else have incompatible types
--> src/main.rs:4:18
|
4 | let number = if condition {
| ^ expected integral variable, found reference
|
= note: expected type `{integer}`
= note: found type `& ’ static str`
< / code > < / pre >
< p > < code > if< / code > 代码块的表达式返回一个整型,而< code > else< / code > 代码块返回一个字符串。这并不可行因为变量必须只有一个类型。Rust 需要在编译时就确切的知道< code > number< / code > 变量的类型,这样它就可以在编译时证明其他使用< code > number< / code > 变量的地方它的类型是有效的。Rust 并不能够在< code > number< / code > 的类型只能在运行时确定的情况下完成这些功能;这样会使编译器变得更复杂而且只能为代码提供更少的保障,因为它不得不记录所有变量的多种可能的类型。< / p >
2017-02-26 15:11:54 +08:00
< a class = "header" href = "#使用循环重复执行" name = "使用循环重复执行" > < h3 > 使用循环重复执行< / h3 > < / a >
2017-02-14 22:42:54 +08:00
< p > 多次执行一段代码是很常用的。为了这个功能, Rust 提供了多种< strong > 循环< / strong > ( < em > loops< / em > )。一个循环执行循环体中的代码直到结尾并紧接着从回到开头继续执行。为了实验一下循环,让我们创建一个叫做 < em > loops< / em > 的新项目。< / p >
2017-02-13 15:43:15 +08:00
< p > Rust 有三种循环类型:< code > loop< / code > 、< code > while< / code > 和< code > for< / code > 。让我们每一个都试试。< / p >
2017-02-26 15:11:54 +08:00
< a class = "header" href = "#使用loop重复执行代码" name = "使用loop重复执行代码" > < h4 > 使用< code > loop< / code > 重复执行代码< / h4 > < / a >
2017-02-13 15:43:15 +08:00
< p > < code > loop< / code > 关键字告诉 Rust 一遍又一遍的执行一段代码直到你明确要求停止。< / p >
< p > 作为一个例子,将 < em > loops< / 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() {
loop {
println!(" again!" );
}
}
< / code > < / pre >
< p > 当执行这个程序,我们会看到< code > again!< / code > 被连续的打印直到我们手动停止程序.大部分终端都支持一个键盘快捷键, ctrl-C, 来终止一个陷入无限循环的程序。尝试一下: < / p >
< pre > < code class = "language-sh" > $ cargo run
Compiling loops v0.1.0 (file:///projects/loops)
Running `target/debug/loops`
again!
again!
again!
again!
^Cagain!
< / code > < / pre >
< p > 符号< code > ^C< / code > 代表你在这按下了 ctrl-C。在< code > ^C< / code > 之后你可能看到< code > again!< / code > 也可能看不到,这依赖于在接收到终止信号时代码执行到了循环的何处。< / p >
< p > 幸运的是, Rust 提供了另一个更可靠的方式来退出循环。可以使用< code > break< / code > 关键字来告诉程序何时停止执行循环。还记得我们在第二章猜猜看游戏的“猜测正确后退出”部分使用过它来在用户猜对数字赢得游戏后退出程序吗。< / p >
2017-02-26 15:11:54 +08:00
< a class = "header" href = "#while条件循环" name = "while条件循环" > < h4 > < code > while< / code > 条件循环< / h4 > < / a >
2017-02-13 15:43:15 +08:00
< p > 在程序中计算循环的条件也很常见。当条件为真,执行循环。当条件不再为真,调用< code > break< / code > 停止循环。这个循环类型可以通过组合< code > loop< / code > 、< code > if< / code > 、< code > else< / code > 和< code > break< / code > 来实现;如果你喜欢的话,现在就可以在程序中试试。< / p >
< p > 然而,这个模式太常见了所以 Rust 为此提供了一个内建的语言结构,它被称为< code > while< / code > 循环。下面的例子使用了< code > while< / code > :程序循环三次,每次数字都减一。接着,在循环之后,打印出另一个信息并退出:< / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > fn main() {
let mut number = 3;
while number != 0 {
println!(" {}!" , number);
number = number - 1;
}
println!(" LIFTOFF!!!" );
}
< / code > < / pre >
< p > 这个结构消除了很多需要嵌套使用< code > loop< / code > 、< code > if< / code > 、< code > else< / code > 和< code > break< / code > 的代码,这样显得更加清楚。当条件为真就执行,否则退出循环。< / p >
2017-02-26 15:11:54 +08:00
< a class = "header" href = "#使用for遍历集合" name = "使用for遍历集合" > < h4 > 使用< code > for< / code > 遍历集合< / h4 > < / a >
2017-02-13 15:43:15 +08:00
< p > 可以使用< code > while< / code > 结构来遍历一个元素集合,比如数组。例如:< / p >
< figure >
< span class = "filename" > Filename: src/main.rs< / span >
< pre > < code class = "language-rust" > fn main() {
let a = [10, 20, 30, 40, 50];
let mut index = 0;
while index < 5 {
println!(" the value is: {}" , a[index]);
index = index + 1;
}
}
< / code > < / pre >
< figcaption >
< p > Listing 3-5: Looping through each element of a collection using a < code > while< / code > loop< / p >
< / figcaption >
< / figure >
< p > 这里代码对数组中的元素进行计数。它从索引< code > 0< / code > 开始,并接着循环直到遇到数组的最后一个索引(这时,< code > index < 5< / code > 不再为真)。运行这段代码会打印出数组中的每一个元素:< / p >
< pre > < code class = "language-sh" > $ cargo run
Compiling loops v0.1.0 (file:///projects/loops)
Running `target/debug/loops`
the value is: 10
the value is: 20
the value is: 30
the value is: 40
the value is: 50
< / code > < / pre >
< p > 所有数组中的五个元素都如期被打印出来。尽管< code > index< / code > 在某一时刻会到达值< code > 5< / code > ,不过循环在其尝试从数组获取第六个值(会越界)之前就停止了。< / p >
< p > 不过这个过程是容易出错的;如果索引长度不正确会导致程序 panic。这也使程序更慢, 因为编译器增加了运行时代码来对每次循环的每个元素进行条件检查。< / p >
< p > 可以使用< code > for< / code > 循环来对一个集合的每个元素执行一些代码,来作为一个更有效率替代。< code > for< / code > 循环看起来像这样:< / p >
< figure >
< span class = "filename" > Filename: src/main.rs< / span >
< pre > < code class = "language-rust" > fn main() {
let a = [10, 20, 30, 40, 50];
for element in a.iter() {
println!(" the value is: {}" , element);
}
}
< / code > < / pre >
< figcaption >
< p > Listing 3-6: Looping through each element of a collection using a < code > for< / code > loop< / p >
< / figcaption >
< / figure >
< p > 当运行这段代码,将看到与列表 3-5 一样的输出。更为重要的是,我们增强了代码安全性并消除了出现可能会导致超出数组的结尾或遍历长度不够而缺少一些元素这类 bug 机会。< / p >
< p > 例如,在列表 3-5 的代码中,如果从数组< code > a< / code > 中移除一个元素但忘记更新条件为< code > while index < 4< / code > ,代码将会 panic。使用< code > for< / code > 循环的话,就不需要惦记着在更新数组元素数量时修改其他的代码了。< / p >
< p > < code > for< / code > 循环的安全性和简洁性使得它在成为 Rust 中使用最多的循环结构。即使是在想要循环执行代码特定次数时,例如列表 3-5 中使用< code > while< / code > 循环的倒计时例子,大部分 Rustacean 也会使用< code > for< / code > 循环。这么做的方式是使用< code > Range< / code > ,它是标准库提供的用来生成从一个数字开始到另一个数字结束的所有数字序列的类型。< / p >
< p > 下面是一个使用< code > for< / code > 循环来倒计时的例子,它还使用了一个我们还未讲到的方法,< code > rev< / code > ,用来反转 range: < / p >
< p > < span class = "filename" > Filename: src/main.rs< / span > < / p >
< pre > < code class = "language-rust" > fn main() {
for number in (1..4).rev() {
println!(" {}!" , number);
}
println!(" LIFTOFF!!!" );
}
< / code > < / pre >
< p > 这段代码看起来更帅气不是吗?< / p >
2017-02-26 15:11:54 +08:00
< a class = "header" href = "#总结" name = "总结" > < h2 > 总结< / h2 > < / a >
2017-02-13 15:43:15 +08:00
< p > 你做到了!这是一个相当可观的章节:你学习了变量,标量和< code > if< / code > 表达式,还有循环!如果你想要实践本章讨论的概念,尝试构建如下的程序:< / p >
< ul >
< li > 相互转换摄氏与华氏温度< / li >
< li > 生成 n 阶斐波那契数列< / li >
< li > 打印圣诞颂歌“The Twelve Days of Christmas”的歌词, 并利用歌曲中的重复部分( 编写循环) < / li >
< / ul >
< p > 当你准备好继续的时候,让我们讨论一个其他语言中< em > 并不< / em > 常见的概念: 所有权( ownership) 。< / p >
2017-02-11 14:49:51 +08:00
< / div >
<!-- Mobile navigation buttons -->
< a href = "ch03-04-comments.html" class = "mobile-nav-chapters previous" >
< i class = "fa fa-angle-left" > < / i >
< / a >
2017-02-14 22:42:54 +08:00
< a href = "ch04-00-understanding-ownership.html" class = "mobile-nav-chapters next" >
< i class = "fa fa-angle-right" > < / i >
< / a >
2017-02-11 14:49:51 +08:00
< / div >
< a href = "ch03-04-comments.html" class = "nav-chapters previous" title = "You can navigate through the chapters using the arrow keys" >
< i class = "fa fa-angle-left" > < / i >
< / a >
2017-02-14 22:42:54 +08:00
< a href = "ch04-00-understanding-ownership.html" class = "nav-chapters next" title = "You can navigate through the chapters using the arrow keys" >
< i class = "fa fa-angle-right" > < / i >
< / a >
2017-02-11 14:49:51 +08:00
< / 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 >