rust-lang / rust-lang/nomicon

Add an elided bound example in unbounded lifetimes section to make stuff clearer

Open
#422 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
CSS
Stars
2.3k
Forks
325
PR merge metrics
No merged PRs in 30d

Description

In the book written, quoting:

any output lifetimes that don't derive from inputs are unbounded

which refers to the following function of what not to do.

https://github.com/rust-lang/nomicon/blob/ddfa4214487686e91b21aa29afb972c08a8f0d5b/src/unbounded-lifetimes.md?plain=1#L22-L24

My suggestion is to add after the following quote:

The easiest way to avoid unbounded lifetimes is to use lifetime elision at the function boundary.

A "correct" code example to make stuff clearer, something like:

// lifetime 'a in input and output are elided. Bounded.
fn get_str(s: &str) -> &str {
    &s
}

fn main() {
    let soon_dropped = String::from("hello");
    let not_dangling = get_str(&soon_dropped);
    drop(soon_dropped);
    println!("Invalid str: {}", not_dangling); // Invalid str: gӚ_`
}

If this seems logical enough for you, I'd be more then happy to create a PR.

Contributor guide

No contributing guide indexed for this repository

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

Read src/unbounded-lifetimes.md at the linked unbounded lifetimes section, especially the guidance on lifetime elision at function boundaries. Add a clear bounded-lifetime example after that passage, and verify that the documented Rust example accurately demonstrates the intended lifetime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.