concept loop in category rust

appears as: loops, loop, loop
Rust in Action

This is an excerpt from Manning's book Rust in Action.

  • Common control flow mechanisms, such as for loops and the continue keyword.
  • The ampersand (&) prefix to haystack on line 5, e.g. &haystack, may look unfamiliar. The ampersand is acting as a unary operator that returns a reference to haystack. For any type T, the syntax &T returns a read-only reference to T. A useful quirk of references to arrays is that they enable you to iterate over references to the elements of the array with for loops. In Rust terminology, when we use &T we borrow T. A “borrowed” value outlives the scope that it is being used in. The concept of borrowing and its terminology are explained in detail during in chapter 4.

    2.5.4. loop: The basis for Rust’s looping constructs

    Rust contains a loop keyword for providing more control than for and while. loop will execute a code block again and again, never stopping for a tea break. loop continues to execute until a break keyword is encountered or the program is terminated from the outside.

    loop {
      // ...
    }

    loop is often seen when implementing long-running servers.

    loop {
      let requester, request = accept_request();
      let result = process_request(request);
      send_response(requester, result);
    }