E0597
rust
type_error
ai_generated
true
error[E0597]: borrowed value does not live long enough note: argument requires that `value` is borrowed for `'static`
ID: rust/e0597-borrowed-value-from-iterator
87%Fix Rate
82%Confidence
1Evidence
2023-09-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| rustc 1.76 | active | — | — | — |
| rustc 1.77 | active | — | — | — |
Root Cause
Returning a reference to a local variable created inside a function or closure, often from an iterator chain like `.filter()` or `.map()`.
generic中文
返回对在函数或闭包内部创建的局部变量的引用,通常来自迭代器链如 `.filter()` 或 `.map()`。
Official Documentation
https://doc.rust-lang.org/stable/error_codes/E0597.htmlWorkarounds
-
90% success Clone or own the data instead of borrowing: `let cloned = value.clone();` or collect into a `Vec` before returning.
Clone or own the data instead of borrowing: `let cloned = value.clone();` or collect into a `Vec` before returning.
-
85% success Restructure the code to pass ownership: change function signature to return an owned type like `String` instead of `&str`.
Restructure the code to pass ownership: change function signature to return an owned type like `String` instead of `&str`.
中文步骤
克隆或拥有数据而不是借用:`let cloned = value.clone();` 或在返回前收集到 `Vec` 中。
重构代码以传递所有权:将函数签名改为返回拥有类型如 `String` 而不是 `&str`。
Dead Ends
Common approaches that don't work:
-
80% fail
The data still doesn't live long enough; the lifetime bound is a promise the compiler enforces, not a magic fix.
-
95% fail
Can cause dangling pointers and undefined behavior; highly discouraged.