loop

Rust provides a loop keyword to indicate an infinite loop.

The break statement can be used to exit a loop at anytime, whereas the continue statement can be used to skip the rest of the iteration and start a new one.

1. fn main() {
2. let mut count = 0u32;

4. println!("Let's count until infinity!");

6. // Infinite loop
7. loop {
8. count += 1;

10. if count == 3 {
11. println!("three");

13. // Skip the rest of this iteration
14. continue;
15. }

17. println!("{}", count);

19. if count == 5 {
20. println!("OK, that's enough");

22. // Exit this loop
23. break;
24. }
25. }
26. }