In Rust, we can create loops using the loop
keyword. The loop
keyword creates an infinite loop, which means the loop will continue to run until we explicitly tell it to stop.
Here's an example of a basic loop
in Rust:
main.rs65 chars5 lines
The loop above will run continuously, printing "infinite loop!" to the console.
We can also add some control flow to our loop by using keywords like break
and continue
. break
will break out of the loop entirely, while continue
will skip the current iteration and move on to the next.
main.rs271 chars14 lines
In the example above, the loop will run until i
equals 5. When i
is even, we use the continue
keyword to skip that iteration of the loop. When i
is odd, we print its value to the console and increment i
by 1.
Loops are an important part of any programming language. Rust's loop
keyword provides a flexible and easy-to-use way to create loops and add control flow to them.
gistlibby LogSnag