Returning from loops

One of the uses of a loop is to retry an operation until it succeeds. If the
operation returns a value though, you might need to pass it to the rest of the
code: put it after the break, and it will be returned by the loop
expression.

  1. fn main() {
  2. let mut counter = 0;
  3. let result = loop {
  4. counter += 1;
  5. if counter == 10 {
  6. break counter * 2;
  7. }
  8. };
  9. assert_eq!(result, 20);
  10. }