Loop

loop creates an infinite loop that can be controlled with continue, which will skip the current iteration, and break which will break the loop.

If we wanted to print all the multiples of three under 100 we could do something like this:

letmut x = 0;
loop{
x++;
if x >= 100 {
println!("{} is now over 100 so we break the loop", x);
break;
}
if x % 3 != 0 {
continue; // The print statment following this if statement will not be printed
}
println!("{} is divisible by 3", x);
}

Note, this could be simplified by instead checking to make sure that the remainder is 0 and then print the statement accordingly, however I wanted to show how the continue statement worked.

While

while behaves very similarly to many other programming languages. As long as the boolean condition following while is true, the block will run.

If we were to convert our above loop into a while statement it would look like: