06-从循环中返回值

从循环中返回值 — Rust 语言之旅

内容来源 · Tour of Rust / Rust 语言之旅

原文链接: https://tourofrust.com/19_zh-cn.html

从循环中返回值

loop 可以被中断以返回一个值。

示例代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
fn main() {
    let mut x = 0;
    let v = loop {
        x += 1;
        if x == 13 {
            break "found the 13";
        }
    };
    println!("from loop: {}", v);
}
最后修改 August 21, 2026: 更新 (76fc81a2e)