← All notes

Rust book: if expressions, loops, labels, while, and for

Answer: Rust can run some code depending on whether a condition is true, and can run some code repeatedly.

if expressions

An if expression lets you run some code depending on whether a condition is true. The condition must be a bool; Rust will not convert a number, so write if number != 0 rather than if number.

You can handle multiple conditions with else if. Rust executes only the first body whose condition is true.

Because if is an expression, you can use it on the right-hand side of a let statement. The values in each arm must be the same type.

loop, break, continue, and labels

The loop keyword runs a block forever until you stop it. You can place the break keyword within the loop also to tell the program when to stop, and continue to skip over remaining code in this iteration of the loop.

A value written after break is passed out of the loop to the surrounding code.

A label, which must start with a single quote, lets break or continue target that loop instead of the inner one.

while and for

A for loop executes some code for each item in a collection, and is a more concise and safer alternative to while.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Control Flow; The Rust Project Developers, Control Flow (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Control Flow; The Rust Project Developers, Control Flow (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers.

Cite this

Permanent ID: iil:note:mjp72k7z

iinventedlight. 2026. "Rust book: if expressions, loops, labels, while, and for." iinventedlight Library, iil:note:mjp72k7z, version of 2026-11-27. https://iinventedlight.com/library/notes/rust-control-flow/. License: CC-BY-4.0.

@misc{iil_note_mjp72k7z,
  author = {{iinventedlight}},
  title = {{Rust book: if expressions, loops, labels, while, and for}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-27},
  url = {https://iinventedlight.com/library/notes/rust-control-flow/},
  note = {Permanent ID iil:note:mjp72k7z. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-control-flow.md.