E0759
rust
type_error
ai_generated
true
error[E0759]: `X` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
ID: rust/e0759-has-an-anonymous-lifetime
80%Fix Rate
84%Confidence
1Evidence
2024-01-18First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.74.0 | active | — | — | — |
| 1.76.0 | active | — | — | — |
| 1.78.0 | active | — | — | — |
Root Cause
A reference with an elided lifetime (anonymous `'_`) is passed where a `'static` lifetime is required, often in async or thread spawning contexts.
generic中文
具有省略生命周期(匿名 `'_`)的引用被传递到需要 `'static` 生命周期的上下文中,常见于异步或线程生成场景。
Official Documentation
https://doc.rust-lang.org/error_codes/E0759.htmlWorkarounds
-
85% success Use `Arc<T>` and move the `Arc` into the closure/task: `let shared = Arc::new(data); tokio::spawn(async move { /* use shared */ });`
Use `Arc<T>` and move the `Arc` into the closure/task: `let shared = Arc::new(data); tokio::spawn(async move { /* use shared */ });` -
78% success Annotate the lifetime explicitly: `fn foo<'a>(x: &'a str) -> impl 'a + Future { ... }` and ensure the caller owns the data.
Annotate the lifetime explicitly: `fn foo<'a>(x: &'a str) -> impl 'a + Future { ... }` and ensure the caller owns the data.
中文步骤
使用 `Arc<T>` 并将 `Arc` 移入闭包/任务:`let shared = Arc::new(data); tokio::spawn(async move { /* 使用 shared */ });`显式标注生命周期:`fn foo<'a>(x: &'a str) -> impl 'a + Future { ... }` 并确保调用者拥有数据。
Dead Ends
Common approaches that don't work:
-
Adding `'static` lifetime bound to the generic parameter without ensuring the data lives long enough
80% fail
Causes borrow checker errors or requires `unsafe`; often the data is not actually static.
-
Using `Box::leak` to create a `&'static mut` reference
70% fail
Memory leak; only works if the data is truly intended to live forever, rare in practice.