loop expressions

Finally, there is a loop keyword which creates an endless loop. Here you must either break or return to stop the loop:

1. fn main() {
2. let mut x = 10;
3. loop {
4. x = if x % 2 == 0 {
5. x / 2
6. } else {
7. 3 * x + 1
8. };
9. if x == 1 {
10. break;
11. }
12. }
13. println!("Final x: {x}");
14. }
  • Break the loop with a value (e.g. break 8) and print it out.