E4110#
The loop is not expected to yield a value, please remove the argument of the
break
or add an else
branch.
This error occurs when using a break
statement with an argument in a loop that
is not expected to yield a value. This applies to:
while
loopsfor
loops with update expressionsfor .. in ..
iteration loops
These loop constructs do not have a mechanism to return a value from the loop body. If you need to break with a value, you must either:
Remove the argument from the
break
statement if you don’t need to return a value, orAdd an
else
branch to handle the case when the loop completes normally and provide a return value
错误示例#
pub fn f(x: Int) -> Unit {
for i in 0..=x {
break i
// ^^^^^^^^ Error: The for loop is not expected to yield a value, please
// remove the argument of the `break` or add an `else` branch.
}
}
建议#
To fix this error, you can:
Remove the argument from the
break
statement. For example,
pub fn f(x: Int) -> Unit {
for i in 0..=x {
break
}
}
Add an
else
branch to handle the case when the loop completes normally and provide a return value. For example,
pub fn f(x: Int) -> Int {
for i in 0..=x {
break i
} else {
42
}
}