Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Control Flow
控制流

What you’ll learn: How Rust control flow resembles Java in shape but differs in one crucial way: many constructs are expressions, not just statements.
本章将学习: Rust 控制流在外形上和 Java 很像,但有个关键差异:很多结构是表达式,不只是语句。

Difficulty: 🟢 Beginner
难度: 🟢 初级

Java developers usually adapt to Rust control flow quickly. The biggest surprise is that Rust uses expressions much more aggressively.
Java 开发者通常很快就能适应 Rust 的控制流。真正让人一下子拧巴的,往往是 Rust 对表达式的使用要激进得多。

if as an Expression
把 if 当表达式

#![allow(unused)]
fn main() {
let label = if score >= 90 { "great" } else { "ok" };
}

That is closer to a Java ternary expression than to a plain if statement.
这更接近 Java 里的三元表达式,而不是普通 if 语句。

match
match

#![allow(unused)]
fn main() {
let text = match status {
    200 => "ok",
    404 => "missing",
    _ => "other",
};
}

match is central in Rust because it works with enums, options, results, and destructuring.
match 在 Rust 里特别核心,因为它能同时覆盖 enum、option、result 和解构场景。

Loops
循环

JavaRust
while (...)while condition { ... }
enhanced forfor item in items { ... }
for (;;)loop { ... }

loop is the dedicated infinite-loop construct.
loop 是专门的无限循环结构。

Early Exit
提前退出

Rust has return, break, and continue as expected. It also lets break return a value from loop.
Rust 当然也有 return、break、continue,但它还允许 break 从 loop 里直接带值出来。

#![allow(unused)]
fn main() {
let result = loop {
    if ready() {
        break 42;
    }
};
}

Pattern-Oriented Flow
面向模式的控制流

#![allow(unused)]
fn main() {
if let Some(user) = maybe_user {
    println!("{}", user.name);
}
}

This is a very common replacement for “null check plus cast plus use” style logic.
这在 Rust 里非常常见,经常用来替代“先判空、再取值、再使用”的流程。

Advice
建议

  • remember that if, match, and even loop can produce values
    记住 if、match、甚至 loop 都可能产出值。
  • reach for match when branching on enums or structured data
    只要是在 enum 或结构化数据上分支,就优先考虑 match。
  • prefer readable control flow over clever one-liners
    可读的控制流比自作聪明的一行流更重要。

Rust control flow is not hard. The main adjustment is learning to think in expressions and patterns rather than in statements alone.
Rust 控制流本身并不难。真正需要适应的是:思考方式要从“纯语句”转向“表达式和模式”。