It would be useful to be able to label questionmarks, primarily for use with loops:
fn foo<T: Into<i32>>(op: fn(i8, i8) -> Option<T>) {
for i in (0i8..=9i8) {
'a: for j in (0i8..=9i8) {
let k: i32 = op(i, j) 'a?.into();
// do stuff with k
}
}
}
This labeled questionmark would cause the loop to continue/go to the next iteration if the result of op(i, j) is None - or anything that can be considered Into<()>. (is NoneError an Into<()>? I don’t remember)
I believe that still adds another depth of indentation, which is really bad for readability. It also doesn’t tie in with the loops very well and you don’t have much control over where it goes (it just goes to the nearest, literally no control).
This is interesting because with labeled break value it could be a good alternative to try blocks:
// With `try` blocks
let z = try {
let x = foo()?;
let y = bar()?;
x + y
}
// With this proposal
let z = 'a: {
let x = foo() 'a ?;
let y = bar() 'a ?;
Ok(x + y)
}
I definitely would prefer this over try, since it seems to be easier to implement, easier to understand, and I’ve never seen too much use cases that worth to introduce try syntax.
Additionally, it seems to be more flexible:
let z = 'a: {
let x = foo() 'a ?;
let y = bar() 'a ?;
validate(x, y)?;
Ok(x + y)
}
As someone who would have more use for a more general solution, I find it encouraging that other people are looking for it as well. It does keep coming up so I hope at some point something will come along, and that people keep discussing possibilities.