Notes

Fix “borrowed value does not live long enough” in Rust

Keep the owner alive through the last borrow use, or return owned data. Five examples cover locals, borrowed structs, closures, temporaries and iterators.

Find the owner of the borrowed data, the point where it is dropped, and the later use of the reference. Make the owner live long enough or move ownership to the caller. Adding a lifetime annotation does not keep a local allocation alive. Related shapes produce E0515, E0373 or E0716 as well as the familiar E0597.

These examples target rustc 1.100.0, edition 2024. Save each block separately; the examples deliberately reuse names. Diagnostics below are real compiler headlines, trimmed from the complete output.

Return a reference to a local

Failing example:

fn make() -> &'static str { let s = String::from("hi"); &s }
fn main() { println!("{}", make()); }

Output:

error[E0515]: cannot return reference to local variable `s`

The local String is dropped when make returns. Return the owned String; its allocation moves to the caller. A static annotation cannot extend the lifetime of that allocation. See the compiler explanation.

Fixed example:

fn make() -> String { String::from("hi") }
fn main() { println!("{}", make()); }

Store a borrow in a struct

Failing example:

struct View<'a> { text: &'a str }
fn main() { let v; { let s = String::from("hi"); v = View { text: &s }; } println!("{}", v.text); }

Output:

error[E0597]: `s` does not live long enough

View does not own its text. Move the owner outside the inner block, so it outlives the view. If the view must travel independently, store a String instead. The lifetime parameter describes the relationship; it does not allocate or retain the owner. See the compiler explanation.

Fixed example:

struct View<'a> { text: &'a str }
fn main() { let s = String::from("hi"); let v = View { text: &s }; println!("{}", v.text); }

Return a closure that borrows a local

Failing example:

fn make() -> impl Fn() -> usize { let s = String::from("hi"); || s.len() }
fn main() { println!("{}", make()()); }

Output:

error[E0373]: closure may outlive the current function, but it borrows `s`, which is owned by the current function

A returned closure outlives this stack frame. move makes it own the String. Moving an existing reference only moves the reference, so it would not rescue a borrow of an already short-lived owner. See the compiler explanation.

Fixed example:

fn make() -> impl Fn() -> usize { let s = String::from("hi"); move || s.len() }
fn main() { println!("{}", make()()); }

Borrow from a temporary

Failing example:

fn main() { let s = String::from("hi").as_str(); println!("{s}"); }

Output:

error[E0716]: temporary value dropped while borrowed

The String temporary is destroyed at the semicolon. Name the owner first, then call as_str(). Some direct reference initializers get temporary lifetime extension, but arbitrary method calls do not provide that guarantee. See the compiler explanation.

Fixed example:

fn main() { let owner = String::from("hi"); let s = owner.as_str(); println!("{s}"); }

Return an iterator that borrows local storage

Failing example:

fn words() -> impl Iterator<Item = &'static str> { let s = String::from("a b"); s.split_whitespace() }
fn main() { for w in words() { println!("{w}"); } }

Output:

error[E0515]: cannot return value referencing local variable `s`

This iterator yields slices into s; it is not an owned collection of words. Let the caller own the source and return an iterator tied to that borrow. Alternatively, collect owned Strings inside the function and return their owning iterator. See the compiler explanation.

Fixed example:

fn words(s: &str) -> impl Iterator<Item = &str> { s.split_whitespace() }
fn main() { let s = String::from("a b"); for w in words(&s) { println!("{w}"); } }

Compile the fixes yourself

Save any fixed block as fixed.rs and run:

rustc --edition=2024 -Dwarnings fixed.rs -o fixed
./fixed

The local, struct and temporary fixes print hi; the closure prints 2; the iterator prints a and b on separate lines.

Choose ownership by the intended API. Borrow when the caller already owns the input and can preserve it through the last use. Return owned data when the function creates the data. Clone only when you need two independent owners, rather than sprinkling clones around every diagnostic. Box::leak and unjustified static requirements change the resource contract and are poor default fixes for these examples.

Go deeper

The ownership dive covers lifetime relationships and how the compiler checks them; use this checklist when you need to repair a specific failing program.