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

This is just as likely to be written as:

let result = while l<=r {
  let m = (l+r)/2;
  if a[m] < v {
    l = m+1
  } else if a[m] > v {
    r = m-1
  } else {
    break m
  }
};

println!("found at {}", m);
// oops, forgot the not found case

The billion dollar mistake strikes again. As I already said in so many words. Maybe I should have included it in the poll, but Default was neither very prominent nor particularly sane.

I don't think your solution deserves to be called more generic if it only caters to the case of Generator<Return=Result<_, _>> and badly, while actively hurting other possible use cases, like Generator<Return=!>. I actually wouldn't mind some kind of 'ad-hoc' proposal like for? here; if the problem is specific to Result, solve it in a way specific to Result.

In fact, let me sketch something like that here:

for? ITEM in GEN {
    BODY
} exhausted FINAL_VAL {
    FINISH
}

desugars to

let mut gen = GEN.into_generator();
loop {
    match gen.next() {
        Yield(ITEM) => BODY,
        Return(Err(e)) => Err(e)?,
        Return(Ok(FINAL_VAL)) => break FINISH,
    }
}

Bam. Now you can bubble up errors from Results without hassle, and the generator is still free to return something else entirely. What's not to like?

2 Likes