rust-lang / rust-lang/rust-by-example

Explicit annotation: add lifetime lines for more clarity

Open
#1,893 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Handlebars
Stars
8.1k
Forks
1.6k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue centers on the Rust examples failed_borrow() and print_refs(); locate the chapter entries containing them and read the surrounding lifetime explanation first. Confirm with maintainers which lifetime visuals are accurate, then add diagrams clarifying 'static and the relevant scopes, and verify the examples and rendered documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.