break 和 continue

如果需要立即启动 下一次迭代,请使用 continue

If you want to exit any kind of loop early, use break. For loop, this can take an optional expression that becomes the value of the loop expression.

  1. fn main() {
  2. let mut i = 0;
  3. loop {
  4. i += 1;
  5. if i > 5 {
  6. break;
  7. }
  8. if i % 2 == 0 {
  9. continue;
  10. }
  11. println!("{}", i);
  12. }
  13. }