E1018#
There is no continue
in this loop expression, so loop
is useless here.
This error occurs when using a loop
expression that contains no continue
statement. In MoonBit, a loop
without any continue
statements is equivalent
to an block expression that will be executed only once, making the loop
keyword redundant in such cases. You should either add a continue
statement if
you need to restart the loop from the beginning, or use a simpler control flow
construct like match
or if
.
Erroneous example#
fn countdown(n : Int) -> Int {
let mut count = n
loop count {
_..<0 => break
_ => count = count - 1
}
return count
}
Suggestion#
Either add a continue
statement if you need to restart the loop:
fn countdown(n : Int) -> Int {
let mut count = n
loop count {
_..<0 => break
_ => {
continue count - 1
}
}
return count
}
Or use if
/match
you don't need to use continue
:
fn countdown(n : Int) -> Int {
let mut count = n
match count {
_..<0 => ()
_ => count = count - 1
}
return count
}