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;
  3. println!("Let's count until infinity!");
  4. // Infinite loop
  5. loop {
  6. count += 1;
  7. if count == 3 {
  8. println!("three");
  9. // Skip the rest of this iteration
  10. continue;
  11. }
  12. println!("{}", count);
  13. if count == 5 {
  14. println!("OK, that's enough");
  15. // Exit this loop
  16. break;
  17. }
  18. }
  19. }