Skip to content

Lifetime must be 'static, but why? #138408

Open
@timokoesters

Description

@timokoesters

Code

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f9243c5a93b40649aaa99f20dc78d5b0

fn input(_: &'static str) {}

fn main() {
    let x = String::new();
    input(&x);
}

Current output

error[E0597]: `x` does not live long enough
 --> src/main.rs:5:11
  |
4 |     let x = String::new();
  |         - binding `x` declared here
5 |     input(&x);
  |     ------^^-
  |     |     |
  |     |     borrowed value does not live long enough
  |     argument requires that `x` is borrowed for `'static`
6 | }
  | - `x` dropped here while still borrowed

For more information about this error, try `rustc --explain E0597`.

Desired output

Maybe something like

hint: lifetime must be 'static to satisfy function
fn input(_: &'static str) {}

Rationale and extra context

In the example above it is also easy to figure it out yourself, but I ran into a case where the actual function is hidden:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=4c1003f809e67aec7ded5ea736ab0430
pub fn body<T: Into<Body>>(mut self, body: T) -> RequestBuilder
impl From<&'static str> for Body

In this case the message should directly point to the from implementation.

Rust Version

I tried this on the Rust playground on Rust 1.85.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsD-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions