11

Rust contains loop, which is specially designed for infinite looping:

Using the keyword loop, Rust provides a way to loop indefinitely until some terminating statement is reached (like program exit)

I can't think of a single scenario when I would like my loops to run forever. Are there any scenarios when an infinite loop can be useful?

Fusion
  • 5,046
  • 5
  • 42
  • 51
  • 2
    Why downvotes? I believe this is legitimate question, and the answer helped me to undestand the concept. – Fusion Sep 14 '16 at 08:46

3 Answers3

16

loop is often used for loops where we want to break in the middle of the loop's body. That is, you want to do something before testing any condition, then exit the loop if some condition is met, then do something else that must only be done after testing the condition, then repeat. In other languages, this is often rendered as while (true) or for (;;). This situation is common enough that Rust decided to embrace this pattern by reserving a keyword to declare loops that have no entry condition.

Also, Rust doesn't have the equivalent of C's do..while loop, which tests a condition at the end of an iteration, but not before the first iteration. In Rust, you'd emulate it with a loop and an if condition { break } statement at the end of the loop. do..while loops are relatively rare in practice, rarer than "break in the middle" loops in my experience.

Francis Gagné
  • 60,274
  • 7
  • 180
  • 155
8

I often use loop in programs where I expect constant user input, e.g. in command-line games:

fn play() {
    let mut game = Game::new();

    loop { game.turn() }
}

It is also useful due to possible input errors - during each turn they can be printed to the player and the game can be continued as long as the player wants until a victory, failure or resignation breaks the loop.

ljedrz
  • 20,316
  • 4
  • 69
  • 97
  • You mean infinite loop is running during a gameplay whole time? How is it, it does not causes program crash? If i would do this in Python or Javascript I would suffer program/website crash. – Fusion Sep 11 '16 at 18:22
  • 1
    It doesn't, because every turn user input is expected; the CPU is not constantly performing calculations - they are done only after some user feedback is received. – ljedrz Sep 11 '16 at 18:25
  • 2
    I would actually expect some `while let Some(xx) = user.input() { game.turn(); }` here, because I expect that at some point the user will want to quit... – Matthieu M. Sep 11 '16 at 18:32
  • 1
    That is fine when the break conditions are simple. In my case I handled input as `Result`s (and didn't want to exit on an `Error`), had an additional piece of code to evaluate the game status at the beginning of every turn and allowed an exit right after input. – ljedrz Sep 11 '16 at 18:36
2

The main loop in embedded programs. Embedded devices often expect to run for as long as they are switched on. The two main pieces of code are usually event handlers and the main loop. If there is a main loop, it runs forever.

The main loop may have a "wait for interrupt" or it may just constantly dedicate its CPU resources to figuring out a better way to achieve its goal.

Max Murphy
  • 1,701
  • 1
  • 19
  • 29