While experimenting the rust compiler and deliberately writing wrong code, I hit that situation :
fn main() {
let args: Vec<String> = std::env::args().collect();
let n = args.len();
let x = {
print!("");
if n == 0 {
break 1;//trying to break and return 1...
}
...//while not returning an i32 here
};
println!("x={}", x);
}
Among the compilation errors, the diagnostics gives the following hint:
help: consider labeling this block to be able to break within it
7 ~ if n == 0 'block: {
8 ~ break 'block 1;
But as soon as I follow that advice:
fn main() {
let args: Vec<String> = std::env::args().collect();
let n = args.len();
let x = {
print!("");
if n == 0 'block: {
break 'block 1;
}
};
println!("x={}", x);
}
I get
if n == 0 'block: {
| ^^^^^^^ not supported here
The correct solution is obviously
fn main() {
let args: Vec<String> = std::env::args().collect();
let n = args.len();
let x = 'block: {
print!("");
if n == 0 {
break 'block 1;
}
//of course there is still here the expected error: expected integer, found `()`
};
println!("x={}", x);
}
So the initial diagnostic is half wrong. I can see why, it has certainly detected the closest braces, but should it be considered a kind of bug in this case ?