← All notes

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

Sources

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

Permanent ID: iil:note:dbjetcyw

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.},
}

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-while-let.md.