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

It is indeed an elegant workaround.

It might not be able to solve problems in situations that are not yet exist, like the adaption of for to Generator or a generalization of Iterator itself, but the labeled block is orthogonal to them anyway.

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

This proposal is equivalent to declaring myfor as an explicit Option<i32> variable.

fn main() {
    let mut q = 0_i32;
    let myfor = None;           // <----
    for x in 0..10 {
        q += x;
        if q == 6_i32 {
            myfor = Some(q);    // <----
            break;
        }
    }
    arbitrary_middle_code();
    if let Some(t) = myfor {    // <----
        process(t);
    } else {
        something_else();
    }
}
4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.