E0502: Break with value
| Code | Category | Stability |
|---|---|---|
E0502 | Control flow | Permanent |
Explanation
Rue loops currently produce only () when exited by break. A break therefore cannot carry a value operand: break expression is rejected even when the surrounding loop appears in a value position.
Likely cause
Code used break value expecting that value to become the result of a loop. Store the value in a mutable binding before a valueless break, or restructure the code so the desired value is produced after the loop.
Examples
Break with a value operand
fn main() -> i32 {
loop {
break 42;
}
}
Store the value before breaking
fn main() -> i32 {
let mut answer = 0;
loop {
answer = 42;
break;
}
answer
}