Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit annotation: add lifetime lines for more clarity #1893

Open
barower opened this issue Oct 28, 2024 · 0 comments
Open

Explicit annotation: add lifetime lines for more clarity #1893

barower opened this issue Oct 28, 2024 · 0 comments

Comments

@barower
Copy link

barower commented Oct 28, 2024

The whole idea of creating a function that accepts lifetime annotation that does not correspond to any input nor output, but still has an effect on references inside it made me rethink again how lifetimes work on fundamental level.

I think this chapter would benefit from more visual explanation. For example, comment under failed_borrow(); line specifies that its 'a "defaults to 'static", but 'static is a tricky concept for beginners, since it doesn't necessarily mean "entire duration of a program". I'm honestly not sure if in this particular case lines should look like this:

fn main() {  ---------------------------------| `failed_borrow()'s` `'a` AKA `'static`
    let (four, nine) = (4, 9);                |
                                              |
    print_refs(&four, &nine);                 |
                                              |
    failed_borrow();                          |
} --------------------------------------------|

Or like this:

fn main() {
    let (four, nine) = (4, 9);

    print_refs(&four, &nine);

    failed_borrow(); -------------------| `failed_borrow()'s` `'a` AKA `'static`
} --------------------------------------|

When it comes to failed_borrow()'s body, does it even make sense to draw lines inside it, since the lifetime "goes outside"? Or maybe it could be drawn like this:

fn failed_borrow<'a>() { ---------------------------------| `'a` AKA `'static`
    let _x = 12;                                          |
                                                          |
    // ERROR: `_x` does not live long enough              |
    let _y: &'a i32 = &_x;  ----|`&_x`'s lifetime         |
}   ----------------------------| oops!                   |
                                                          |
                                                         ... till the end of the program

Maybe print_refs()'s example could also benefit from this kind of visual feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant