Sisu
E0082

break or continue in a loop header

Error

Built from the repository at commit a8614dc.

The header of a loop is the condition of a while or while let, and the source of a for. A break or continue there is an error, also inside a block expression in the header.

Why

A header is not the loop's body. A while condition runs before every pass, and a for source runs once, before the loop starts. In a for source, a break would leave the loop around the for, not the for itself. Put the jump in the body. A loop inside the header has a body of its own, and a break there is fine. A labeled break or continue there that names a loop outside the header is an error too, since it would leave the header.

Broken

fn main() {
    let mut i = 0
    while {
        i += 1
        if i == 3 {
            break
        }
        i < 5
    } {
        print(i)
    }
}

What sisu says

error[E0082]: `break` cannot appear in a loop header
 --> E0082.sisu:6:13
  |
3 |     while {
  |     ----- the header of this loop
...
6 |             break
  |             ^^^^^

sisu: 1 error
sisu: for more on a code, run `sisu explain <code>`: E0082

Fixed

fn main() {
    let mut i = 0
    while i < 5 {
        i += 1
        if i == 3 {
            break
        }
        print(i)
    }
}

Related

  • E0038: break or continue outside a loop.