02-loop 循环
loop 循环 — Rust By Example
译文 · 基于 Rust By Example
原文链接: https://doc.rust-lang.org/stable/rust-by-example/flow_control/loop.html
loop 循环
Rust 提供了 loop 关键字来表示一个无限循环。
可以使用 break 语句在任何时候退出一个循环,还可以使用 continue 跳过循环体的剩余部分并开始下一轮循环。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| fn main() {
let mut count = 0u32;
println!("Let's count until infinity!");
// 无限循环
loop {
count += 1;
if count == 3 {
println!("three");
// 跳过这次迭代的剩下内容
continue;
}
println!("{}", count);
if count == 5 {
println!("OK, that's enough");
// 退出循环
break;
}
}
}
//Let's count until infinity!
//1
//2
//three
//4
//5
//OK, that's enough
|
嵌套循环和标签 — Rust By Example
从 loop 循环返回 — Rust By Example