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
- id: C1 claim: if chooses a block based on whether a condition is true. source: S2 evidence: "run some code depending on whether a condition is"
- id: C2 claim: break can pass a value out of a loop. source: S1 evidence: "returned out of the loop so that you"
- id: C3 claim: A loop label must start with a single quote. source: S2 evidence: "Loop labels must begin"
- id: C4 claim: while repeats while its condition stays true. source: S1 evidence: "Streamlining Conditional Loops with while"
- id: C5 claim: for visits each item in a collection. source: S2 evidence: "for each item in a collection"
Sources
- [S1] Control Flow. The Rust Project Developers. https://doc.rust-lang.org/book/ch03-05-control-flow.html . License: MIT OR Apache-2.0. Accessed 2026-11-27.
- [S2] Control Flow (source). The Rust Project Developers. https://raw.githubusercontent.com/rust-lang/book/main/src/ch03-05-control-flow.md . License: MIT OR Apache-2.0. Accessed 2026-11-27.
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
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.},
}
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.
- rust-control-flow.schema.json (schema; CC-BY-4.0)
- rust-control-flow.table.json (table; CC-BY-4.0)
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.