loops can break with a value

Minimum Rust version: 1.19

loops can now break with a value:

  1. #![allow(unused_variables)]
  2. fn main() {
  3. // old code
  4. let x;
  5. loop {
  6. x = 7;
  7. break;
  8. }
  9. // new code
  10. let x = loop { break 7; };
  11. }

Rust has traditionally positioned itself as an “expression orientedlanguage”, that is, most things are expressions that evaluate to a value,rather than statements. loop stuck out as strange in this way, as it waspreviously a statement.

For now, this only applies to loop, and not things like while or for.See the rationale for this decision in RFC issue #1767.