Pre-RFC: Break with value in for/while loops

Not a fan of this, for the following reasons:

  • Default is a bad idea on its own merits
  • Default::default() can potentially have side effects, which would not be apparent from the loop code
  • Your proposal fails to distinguish exhausting the loop from break Default::default()
  • Evaluating to Option<_> is no less expressive, as you can still do .or_default()

Well, you did post it as a pre-RFC, and it did include break semantics (which you got confused with someone else's continue proposal). I don't like the continue proposal either, but for different reasons.

Also, consider that with the generator proposal, if you have a generator which returns values indefinitely, i.e. a Generator<Return=!>, then a for looping over it cannot meaningfully use break at all (unless it's out of an outer block). I'd argue that breaking out of an infinite loop is a quite important operation that shouldn't be too awkward to express.

1 Like