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.

Difficulty: 🟢 Beginner

Java developers usually adapt to Rust control flow quickly. The biggest surprise is that Rust uses expressions much more aggressively.

if as an Expression

#![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.

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.

Loops

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

loop is the dedicated infinite-loop construct.

Early Exit

Rust has return, break, and continue as expected. It also lets break return a value from 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.

Advice

  • remember that if, match, and even loop can produce values
  • reach for match when branching on enums or structured data
  • 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.