4.4 循环

循环 — Comprehensive Rust

译文 · 基于 Comprehensive Rust

原文链接: https://google.github.io/comprehensive-rust/control-flow-basics/loops.html

4.4 循环

Rust 中有三个循环关键字:while、loop 和 for:

while

while 关键字 的用法与其他语言非常相似:只要条件为真,就执行循环体。

1
2
3
4
5
6
7
8
9
// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
fn main() {
    let mut x = 200;
    while x >= 10 {
        x = x / 2;
    }
    dbg!(x);
}

4.4.1 `for` 循环

01-for 循环 — Comprehensive Rust

4.4.2 `loop` 循环

02-loop 循环 — Comprehensive Rust

最后修改 August 11, 2026: 更新 (70a5af133)