# while-let-loop

Prefer while-let over an unconditional pattern loop

An unconditional loop that continues only while one pattern matches hides its condition in the body.
Instead, you SHOULD place the successful pattern and value in a while-let condition.

The matched expression remains evaluated once at the start of each iteration.

- Category: style
- Level: warning
- Fix: suggestion
- Scope: module

## Reported

```ds title="main.ds"
declare function next(): Result<int32, void>;

function consume(): void {
    loop {
        match (next()) {
            Ok { value } => {
                value;
            }
            Err { error: _ } => break
        }
    }
}
```

## Accepted

```ds title="main.ds"
declare function next(): Result<int32, void>;

function consume(): void {
    while (let Ok { value } = next()) {
        value;
    }
}
```

## Prior art

- [Clippy · while_let_loop](https://rust-lang.github.io/rust-clippy/master/index.html#while_let_loop)

[language/linter/src/rules/style/while_let_loop.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/style/while_let_loop.rs#L8)
