continue in while cause unexpected error. Here is the example codes
fun a() {
let index: u64 = 0;
let sum = 0;
while (index < 10) {
if (index % 2 == 0) {
sum = sum + index
} else {
index = index + 1;
continue
};
index = index + 1;
};
std::debug::print(&sum);
}
This causes the following error at index: In a loop, this typically means it was moved in the first iteration, and is not available by the second iteration.
I added copy to fix this, but _index is not updated.
fun a() {
let _index: u64 = 0;
let sum = 0;
while (_index < 10) {
std::debug::print(&_index); // _index is not increasing when it get 1
if (_index % 2 == 0) {
sum = sum + _index
} else {
_index = copy _index + 1;
continue
};
_index = _index + 1;
};
std::debug::print(&sum);
}
Most weird part is when I add &index; before continue, it works as expected.
fun a() {
let index: u64 = 0;
let sum = 0;
while (index < 10) {
if (index % 2 == 0) {
sum = sum + index
} else {
index = index + 1;
&index;
continue
};
index = index + 1;
};
std::debug::print(&sum); // print 20 here
}
I think that continue makes some error. Without continue, all the codes above works.
🐛 Bug
continue
inwhile
cause unexpected error. Here is the example codesThis causes the following error at
index
:In a loop, this typically means it was moved in the first iteration, and is not available by the second iteration.
I added
copy
to fix this, but_index
is not updated.Most weird part is when I add
&index;
before continue, it works as expected.I think that continue makes some error. Without continue, all the codes above works.