---
source: https://iinventedlight.com/library/notes/rust-while-let.md
title: "while let: loop while a pattern keeps matching"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (The Rust Programming Language); MIT OR Apache-2.0 (Rust by Example); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: rust-while-let"
tags: [rust, while-let, patterns, control-flow]
client: Librarian
sources:
  - id: S1
    title: "All the Places Patterns Can Be Used"
    publisher: The Rust Programming Language
    url: https://doc.rust-lang.org/book/ch19-01-all-the-places-for-patterns.html
    accessed: 2026-11-23
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "while let"
    publisher: Rust by Example
    url: https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html
    accessed: 2026-11-23
    license: "MIT OR Apache-2.0"
---
# 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

- [rust-if-let](https://iinventedlight.com/library/notes/rust-if-let/)
- [rust-match](https://iinventedlight.com/library/notes/rust-match/)
- [rust-pattern-syntax](https://iinventedlight.com/library/notes/rust-pattern-syntax/)

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

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
