控制流

根据条件是否为 true 来运行某些代码,以及在条件为 true 时重复运行某些代码的能力是大多数编程语言的基本构建块。控制 Rust 代码执行流程的最常见的结构是 if 表达式和循环。

if 表达式

if 表达式允许您根据条件分支您的代码。您提供一个条件,然后声明:“如果满足此条件,则运行此代码块。如果条件不满足,则不运行此代码块。”

在您的 projects 目录中创建一个名为 branches 的新项目,以探索 if 表达式。在 src/main.rs 文件中,输入以下内容

文件名:src/main.rs

fn main() {
    let number = 3;

    if number < 5 {
        println!("condition was true");
    } else {
        println!("condition was false");
    }
}

所有 if 表达式都以关键字 if 开头,后跟一个条件。在本例中,条件检查变量 number 的值是否小于 5。我们将如果条件为 true 时要执行的代码块放在条件后面的花括号内。与 if 表达式中的条件关联的代码块有时称为 *分支*,就像我们在第 2 章的“将猜测与秘密数字进行比较”部分中讨论的 match 表达式中的分支一样。

可选地,我们还可以包含一个 else 表达式,我们在这里选择这样做,以便在条件计算结果为 false 时为程序提供一个可供执行的替代代码块。如果您不提供 else 表达式,并且条件为 false,则程序将跳过 if 代码块,然后继续执行下一段代码。

尝试运行此代码;您应该看到以下输出

$ cargo run
   Compiling branches v0.1.0 (file:///projects/branches)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.31s
     Running `target/debug/branches`
condition was true

让我们尝试将 number 的值更改为使条件为 false 的值,看看会发生什么

fn main() {
    let number = 7;

    if number < 5 {
        println!("condition was true");
    } else {
        println!("condition was false");
    }
}

再次运行该程序,并查看输出

$ cargo run
   Compiling branches v0.1.0 (file:///projects/branches)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.31s
     Running `target/debug/branches`
condition was false

还值得注意的是,此代码中的条件 *必须* 是 bool。如果条件不是 bool,我们会得到一个错误。例如,尝试运行以下代码

文件名:src/main.rs

fn main() {
    let number = 3;

    if number {
        println!("number was three");
    }
}

这次 if 条件的计算结果为 3,Rust 抛出一个错误

$ cargo run
   Compiling branches v0.1.0 (file:///projects/branches)
error[E0308]: mismatched types
 --> src/main.rs:4:8
  |
4 |     if number {
  |        ^^^^^^ expected `bool`, found integer

For more information about this error, try `rustc --explain E0308`.
error: could not compile `branches` (bin "branches") due to 1 previous error

该错误表明 Rust 期望得到一个 bool,但却得到了一个整数。与 Ruby 和 JavaScript 等语言不同,Rust 不会自动尝试将非布尔类型转换为布尔值。您必须明确地始终为 if 提供一个布尔值作为其条件。例如,如果我们希望仅当数字不等于 0 时才运行 if 代码块,我们可以将 if 表达式更改为以下内容

文件名:src/main.rs

fn main() {
    let number = 3;

    if number != 0 {
        println!("number was something other than zero");
    }
}

运行此代码将打印 number was something other than zero

使用 else if 处理多个条件

您可以通过在 else if 表达式中组合 ifelse 来使用多个条件。例如

文件名:src/main.rs

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");
    }
}

此程序有四种可能的路径可以采用。运行它之后,您应该看到以下输出

$ cargo run
   Compiling branches v0.1.0 (file:///projects/branches)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.31s
     Running `target/debug/branches`
number is divisible by 3

当此程序执行时,它会依次检查每个 if 表达式,并执行条件计算结果为 true 的第一个主体。请注意,即使 6 可以被 2 整除,我们也没有看到输出 number is divisible by 2,也没有看到 else 代码块中的 number is not divisible by 4, 3, or 2 文本。这是因为 Rust 只执行第一个 true 条件的代码块,一旦找到一个,它甚至不会检查其余的。

使用太多的 else if 表达式会使您的代码变得混乱,因此如果您有多个表达式,您可能需要重构您的代码。第 6 章描述了一种强大的 Rust 分支结构,称为 match,适用于这些情况。

let 语句中使用 if

因为 if 是一个表达式,我们可以在 let 语句的右侧使用它,将结果分配给一个变量,如清单 3-2 所示。

文件名:src/main.rs
fn main() {
    let condition = true;
    let number = if condition { 5 } else { 6 };

    println!("The value of number is: {number}");
}
清单 3-2:将 if 表达式的结果分配给变量

number 变量将根据 if 表达式的结果绑定到一个值。运行此代码以查看会发生什么

$ cargo run
   Compiling branches v0.1.0 (file:///projects/branches)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.30s
     Running `target/debug/branches`
The value of number is: 5

请记住,代码块的计算结果为其中的最后一个表达式,并且数字本身也是表达式。在这种情况下,整个 if 表达式的值取决于执行哪个代码块。这意味着每个 if 分支可能产生的结果的值必须是相同的类型;在清单 3-2 中,if 分支和 else 分支的结果都是 i32 整数。如果类型不匹配,如下例所示,我们将得到一个错误

文件名:src/main.rs

fn main() {
    let condition = true;

    let number = if condition { 5 } else { "six" };

    println!("The value of number is: {number}");
}

当我们尝试编译此代码时,我们将得到一个错误。ifelse 分支具有不兼容的值类型,Rust 指示了在程序中找到问题的确切位置

$ cargo run
   Compiling branches v0.1.0 (file:///projects/branches)
error[E0308]: `if` and `else` have incompatible types
 --> src/main.rs:4:44
  |
4 |     let number = if condition { 5 } else { "six" };
  |                                 -          ^^^^^ expected integer, found `&str`
  |                                 |
  |                                 expected because of this

For more information about this error, try `rustc --explain E0308`.
error: could not compile `branches` (bin "branches") due to 1 previous error

if 代码块中的表达式的计算结果为一个整数,而 else 代码块中的表达式的计算结果为一个字符串。这是行不通的,因为变量必须具有单一类型,并且 Rust 需要在编译时明确地知道 number 变量是什么类型。知道 number 的类型可以使编译器验证该类型在我们使用 number 的任何位置都是有效的。如果 number 的类型仅在运行时确定,Rust 将无法做到这一点;如果编译器必须跟踪任何变量的多个假设类型,则编译器会更复杂,并且会对代码做出更少的保证。

使用循环进行重复

多次执行一个代码块通常很有用。对于此任务,Rust 提供了几个 *循环*,它们将运行到循环体内的代码末尾,然后立即从头开始。为了进行循环实验,让我们创建一个名为 loops 的新项目。

Rust 有三种循环:loopwhilefor。让我们尝试每一个。

使用 loop 重复代码

关键字 loop 告诉 Rust 永远重复执行一个代码块,或者直到您明确告诉它停止为止。

例如,将您的 loops 目录中的 src/main.rs 文件更改为如下所示

文件名:src/main.rs

fn main() {
    loop {
        println!("again!");
    }
}

当我们运行此程序时,我们将看到 again! 持续重复打印,直到我们手动停止程序。大多数终端都支持键盘快捷键 ctrl-c 来中断卡在连续循环中的程序。尝试一下

$ cargo run
   Compiling loops v0.1.0 (file:///projects/loops)
    Finished dev [unoptimized + debuginfo] target(s) in 0.29s
     Running `target/debug/loops`
again!
again!
again!
again!
^Cagain!

符号 ^C 表示您按下 ctrl-c 的位置。您可能会也可能不会看到 ^C 之后打印的单词 again!,具体取决于代码在收到中断信号时在循环中的位置。

幸运的是,Rust 还提供了一种使用代码跳出循环的方法。您可以将 break 关键字放在循环内,以告诉程序何时停止执行循环。回想一下,我们在“猜对之后退出”第 2 章的部分中,在用户通过猜对正确的数字赢得游戏时使用此方法退出程序。

我们还在猜谜游戏中使用了 continue,它在循环中告诉程序跳过此循环迭代中的任何剩余代码并进入下一个迭代。

从循环返回值

loop 的用途之一是重试您知道可能会失败的操作,例如检查线程是否已完成其工作。您可能还需要将该操作的结果从循环传递到代码的其余部分。为此,您可以在用于停止循环的 break 表达式之后添加要返回的值;该值将从循环中返回,以便您可以使用它,如下所示

fn main() {
    let mut counter = 0;

    let result = loop {
        counter += 1;

        if counter == 10 {
            break counter * 2;
        }
    };

    println!("The result is {result}");
}

在循环之前,我们声明一个名为 counter 的变量,并将其初始化为 0。然后我们声明一个名为 result 的变量来保存从循环返回的值。在循环的每次迭代中,我们将 1 添加到 counter 变量,然后检查 counter 是否等于 10。当它等于时,我们使用带值 counter * 2break 关键字。循环结束后,我们使用分号来结束将值分配给 result 的语句。最后,我们打印 result 中的值,在本例中为 20

您还可以从循环内部 return。虽然 break 只退出当前循环,但 return 始终退出当前函数。

用于区分多个循环的循环标签

如果您在循环中嵌套了循环,则 breakcontinue 将应用于该点最内层的循环。您可以在循环上可选地指定一个 *循环标签*,然后将其与 breakcontinue 一起使用,以指定这些关键字适用于标记的循环而不是最内层的循环。循环标签必须以单引号开头。这是一个带有两个嵌套循环的示例

fn main() {
    let mut count = 0;
    'counting_up: loop {
        println!("count = {count}");
        let mut remaining = 10;

        loop {
            println!("remaining = {remaining}");
            if remaining == 9 {
                break;
            }
            if count == 2 {
                break 'counting_up;
            }
            remaining -= 1;
        }

        count += 1;
    }
    println!("End count = {count}");
}

外循环具有标签 'counting_up,它将从 0 向上计数到 2。没有标签的内循环从 10 向下计数到 9。第一个不指定标签的 break 只会退出内循环。break 'counting_up; 语句将退出外循环。此代码打印

$ cargo run
   Compiling loops v0.1.0 (file:///projects/loops)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.58s
     Running `target/debug/loops`
count = 0
remaining = 10
remaining = 9
count = 1
remaining = 10
remaining = 9
count = 2
remaining = 10
End count = 2

使用 while 进行条件循环

程序通常需要在循环内评估条件。当条件为 true 时,循环运行。当条件不再为 true 时,程序会调用 break,停止循环。可以使用 loopifelsebreak 的组合来实现这样的行为;如果您愿意,您现在可以在程序中尝试一下。然而,这种模式非常常见,Rust 为此内置了一个语言结构,称为 while 循环。在清单 3-3 中,我们使用 while 将程序循环三次,每次都向下计数,然后在循环之后,打印一条消息并退出。

文件名:src/main.rs
fn main() {
    let mut number = 3;

    while number != 0 {
        println!("{number}!");

        number -= 1;
    }

    println!("LIFTOFF!!!");
}
清单 3-3:使用 while 循环在条件为 true 时运行代码

此结构消除了如果使用 loopifelsebreak 所必需的许多嵌套,并且更清晰。当条件评估为 true 时,代码会运行;否则,它会退出循环。

使用 for 循环遍历集合

您还可以使用 while 结构循环遍历集合(例如数组)的元素。例如,清单 3-4 中的循环打印数组 a 中的每个元素。

文件名:src/main.rs
fn main() {
    let a = [10, 20, 30, 40, 50];
    let mut index = 0;

    while index < 5 {
        println!("the value is: {}", a[index]);

        index += 1;
    }
}
清单 3-4:使用 while 循环遍历集合中的每个元素

在这里,代码在数组中向上计数元素。它从索引 0 开始,然后循环直到到达数组中的最后一个索引(即,当 index < 5 不再为 true 时)。运行此代码将打印数组中的每个元素

$ cargo run
   Compiling loops v0.1.0 (file:///projects/loops)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.32s
     Running `target/debug/loops`
the value is: 10
the value is: 20
the value is: 30
the value is: 40
the value is: 50

所有五个数组值都按预期出现在终端中。即使 index 在某个时候达到值 5,循环也会在尝试从数组中获取第六个值之前停止执行。

然而,这种方法容易出错;如果索引值或测试条件不正确,我们可能会导致程序崩溃。例如,如果您将 a 数组的定义更改为具有四个元素,但忘记将条件更新为 while index < 4,则代码将崩溃。它也很慢,因为编译器会添加运行时代码来执行条件检查,以检查索引是否在每次循环迭代时都在数组的边界内。

作为更简洁的替代方案,您可以使用 for 循环并为集合中的每个项目执行一些代码。for 循环类似于清单 3-5 中的代码。

文件名:src/main.rs
fn main() {
    let a = [10, 20, 30, 40, 50];

    for element in a {
        println!("the value is: {element}");
    }
}
列表 3-5:使用 for 循环遍历集合中的每个元素

当我们运行这段代码时,我们会看到与列表 3-4 中相同的输出。更重要的是,我们现在提高了代码的安全性,并消除了由于超出数组末尾或未达到足够远而遗漏某些项目而可能导致的错误。

使用 for 循环,如果您更改数组中的值数量,则无需像在列表 3-4 中使用的方法那样更改任何其他代码。

for 循环的安全性和简洁性使其成为 Rust 中最常用的循环结构。即使在你想运行代码一定次数的情况下,就像在列表 3-3 中使用 while 循环的倒计时示例一样,大多数 Rustaceans 也会使用 for 循环。这样做的方法是使用标准库提供的 Range,它生成从一个数字开始到另一个数字结束之前的所有数字序列。

以下是使用 for 循环和我们尚未讨论的另一种方法 rev(用于反转范围)的倒计时代码

文件名:src/main.rs

fn main() {
    for number in (1..4).rev() {
        println!("{number}!");
    }
    println!("LIFTOFF!!!");
}

这段代码是不是更简洁了?

总结

你成功了!这是一个相当大的章节:你学习了变量、标量和复合数据类型、函数、注释、if 表达式和循环!为了练习本章讨论的概念,尝试构建程序来执行以下操作

  • 在华氏温度和摄氏温度之间转换。
  • 生成第 n 个斐波那契数。
  • 打印圣诞颂歌“圣诞节的十二天”的歌词,利用歌曲中的重复部分。

当你准备好继续前进时,我们将讨论 Rust 中一个在其他编程语言中 常见的概念:所有权。