Problem with lifetime

Reading "The Rustonomicon" I found this example/explanation:

Actually passing references to outer scopes will cause Rust to infer a larger lifetime:
let x = 0;
let z;
let y = &x;
z = y;

The book explains that preceeding statements are equivalent to the following:

'a: {
    let x: i32 = 0;
    'b: {
        let z: &'b i32;
        'c: {
            // Must use 'b here because the reference to x is
            // being passed to the scope 'b.
            let y: &'b i32 = &'b x;
            z = y;
        }
    }
}

Can you explain the reason behind this rule?
What is the reason that led to formulating it?
I'm an absolute beginner but I want to try to understand the rationale rather than just memorizing.
Thank you for your time

For the assignment z = y to work, the lifetime of y must be at least as long as that of z -- otherwise you'd risk having a dangling reference. So 'b is the smallest possible lifetime for y.

I think the example is a bit outdated because with the non-lexical lifetimes feature, all three lifetimes are same -- they end after the same instruction z = y.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.