while let: loop while a pattern keeps matching
Answer: A while let loop can run for as long as a pattern continues to match. Similar to if let, while let can make awkward match sequences more tolerable.
A conditional loop on a pattern
Similar in construction to if let, the while let loop can run for as long as a pattern continues to match.
The book walks through a channel receiver: while let Ok(value) = rx.recv() prints each value for as long as recv returns Ok.
While a sender still exists, recv yields Ok(value) for each message. After the sender side disconnects, it produces an Err.
A shorter alternative to loop and match
Rust by Example says while let, like if let, can make awkward match sequences more tolerable.
The example uses while let Some(i) = optional.
Using while let makes this sequence much nicer and does not require explicitly handling the failing case.
if let had additional optional else or else if clauses. while let does not have these.
See also
Claims
- id: C1 claim: while let runs for as long as a pattern continues to match. source: S1 evidence: "run for as long as a pattern continues to match"
- id: C2 claim: The book example loops on Ok values from rx.recv(). source: S1 evidence: "while let Ok(value) = rx.recv()"
- id: C3 claim: recv yields Err once the sender disconnects. source: S1 evidence: "produces an Err once the sender side disconnects"
- id: C4 claim: while let can replace awkward match sequences. source: S2 evidence: "while let can make awkward match sequences more tolerable"
- id: C5 claim: while let has no else or else if clauses. source: S2 evidence: "does not have these"
Sources
- [S1] All the Places Patterns Can Be Used. The Rust Programming Language. https://doc.rust-lang.org/book/ch19-01-all-the-places-for-patterns.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
- [S2] while let. Rust by Example. https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html . License: MIT OR Apache-2.0. Accessed 2026-11-23.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, All the Places Patterns Can Be Used; Rust by Example, while let. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/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 Programming Language, All the Places Patterns Can Be Used; Rust by Example, while let. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/rust-by-example/master/LICENSE-MIT). Copyright the Rust Project Developers.
Cite this
iinventedlight. 2026. "while let: loop while a pattern keeps matching." iinventedlight Library, iil:note:dbjetcyw, version of 2026-11-23. https://iinventedlight.com/library/notes/rust-while-let/. License: CC-BY-4.0.
@misc{iil_note_dbjetcyw,
author = {{iinventedlight}},
title = {{while let: loop while a pattern keeps matching}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/rust-while-let/},
note = {Permanent ID iil:note:dbjetcyw. 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-while-let.schema.json (schema; CC-BY-4.0)
- rust-while-let.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-while-let.md.