Are block labels the same as lifetimes, and should they be?

50 points donaldhobson 45 comments

So rust now has labeled blocks. https://blog.rust-lang.org/2022/11/03/Rust-1.65.0/#break-from-labeled-blocks

These look to me to be very similar to lifetimes. Same ' prefix. Same way they are only valid inside a block.

But as it is, we can't currently treat block labels like lifetimes. In particular, block labels can't be passed as generic parameters into functions. So this code doesn't run.

It's a common practice in programming to split one big function into lots of smaller ones. Currently the existence of break (or return ) statements in a function interferes with this process of separating out sub-functions.

One difficulty is that it isn't quite clear how this interacts with recursive functions. But doing the same thing as with lifetimes (whatever that is) might work.

Basically, this code doesn't currently work, but I think it should.

fn breaker<'a>()->!{
        break 'a 7;
}
fn main() {
    println!("Hello, world!");
    let result = 'block: {
    breaker::<'block>();
    3
    };
    println!("{}",result);//prints 7
}