控制流(control flow)
If
If是分支 (branch) 的一种特殊形式,也可以使用else和else if。
与C语言不同的是,逻辑条件不需要用小括号括起来,但是条件后面必须跟一个代码块。
Rust中的if是一个表达式 (expression),可以赋给一个变量:
1. let x = 5;
3. let y = if x == 5 { 10 } else { 15 };
Rust是基于表达式的编程语言,有且仅有两种语句 (statement):
- 声明语句 (declaration statement),比如进行变量绑定的
let语句。 - 表达式语句 (expression statement),它通过在末尾加上分号
;来将表达式变成语句, 丢弃该表达式的值,一律返回unit()。
表达式如果返回,总是返回一个值,但是语句不返回值或者返回(),所以以下代码会报错:
1. let y = (let x = 5);
3. let z: i32 = if x == 5 { 10; } else { 15; };
值得注意的是,在Rust中赋值 (如x = 5) 也是一个表达式,返回unit的值()。
For
Rust中的for循环与C语言的风格非常不同,抽象结构如下:
1. for var in expression {
2. code
3. }
其中expression是一个迭代器 (iterator),具体的例子为0..10 (不包含最后一个值),
或者[0, 1, 2].iter()。
While
Rust中的while循环与C语言中的类似。对于无限循环,Rust有一个专用的关键字loop。
如果需要提前退出循环,可以使用关键字break或者continue,
还允许在循环的开头设定标签 (同样适用于for循环):
1. 'outer: loop {
2. println!("Entered the outer loop");
4. 'inner: loop {
5. println!("Entered the inner loop");
6. break 'outer;
7. }
9. println!("This point will never be reached");
10. }
12. println!("Exited the outer loop");
Match
Rust中的match表达式非常强大,首先看一个例子:
1. let day = 5;
3. match day {
4. 0 | 6 => println!("weekend"),
5. 1 ... 5 => println!("weekday"),
6. _ => println!("invalid"),
7. }
其中|用于匹配多个值,...匹配一个范围 (包含最后一个值),并且_在这里是必须的,
因为match强制进行穷尽性检查 (exhaustiveness checking),必须覆盖所有的可能值。
如果需要得到|或者...匹配到的值,可以使用@绑定变量:
1. let x = 1;
3. match x {
4. e @ 1 ... 5 => println!("got a range element {}", e),
5. _ => println!("anything"),
6. }
使用ref关键字来得到一个引用:
1. let x = 5;
2. let mut y = 5;
4. match x {
5. // the `r` inside the match has the type `&i32`
6. ref r => println!("Got a reference to {}", r),
7. }
9. match y {
10. // the `mr` inside the match has the type `&i32` and is mutable
11. ref mut mr => println!("Got a mutable reference to {}", mr),
12. }
再看一个使用match表达式来解构元组的例子:
1. let pair = (0, -2);
3. match pair {
4. (0, y) => println!("x is `0` and `y` is `{:?}`", y),
5. (x, 0) => println!("`x` is `{:?}` and y is `0`", x),
6. _ => println!("It doesn't matter what they are"),
7. }
match的这种解构同样适用于结构体或者枚举。如果有必要,还可以使用..来忽略域或者数据:
1. struct Point {
2. x: i32,
3. y: i32,
4. }
6. let origin = Point { x: 0, y: 0 };
8. match origin {
9. Point { x, .. } => println!("x is {}", x),
10. }
12. enum OptionalInt {
13. Value(i32),
14. Missing,
15. }
17. let x = OptionalInt::Value(5);
19. match x {
20. // 这里是 match 的 if guard 表达式,我们将在以后的章节进行详细介绍
21. OptionalInt::Value(i) if i > 5 => println!("Got an int bigger than five!"),
22. OptionalInt::Value(..) => println!("Got an int!"),
23. OptionalInt::Missing => println!("No such luck."),
24. }
此外,Rust还引入了if let和while let进行模式匹配:
1. let number = Some(7);
2. let mut optional = Some(0);
4. // If `let` destructures `number` into `Some(i)`, evaluate the block.
5. if let Some(i) = number {
6. println!("Matched {:?}!", i);
7. } else {
8. println!("Didn't match a number!");
9. }
11. // While `let` destructures `optional` into `Some(i)`, evaluate the block.
12. while let Some(i) = optional {
13. if i > 9 {
14. println!("Greater than 9, quit!");
15. optional = None;
16. } else {
17. println!("`i` is `{:?}`. Try again.", i);
18. optional = Some(i + 1);
19. }
20. }
