Fix Rust E0597 "`x` does not live long enough" — a Block Scope, a Stored Reference, a Struct Field, and a Closure Capture
A reference outlives the value it points at, and the compiler stops the build rather than leave a dangling reference. This guide separates the four shapes that produce E0597 — a block scope, a stored reference, a struct field, and a closure capture — and matches each one to moving the binding outward, moving the use inward, or handing back an owned value.
Written November 22, 2026 · Tested with Rust 1.99.0 · 15 min read
- The rule underneath: the referent must outlive every use of the borrow
- How to read the diagnostic
- The first failure: a reference used after its block ends
- The second failure: a reference stored in a variable that outlives it
Written November 22, 2026
Tested with Rust 1.99.0
What this guide is
Error E0597 is a reference that outlives its value. The error codes index states the cause in one line: this error occurs because a value was dropped while it was still borrowed. The guarantee behind it comes from the Rust Book chapter on references and borrowing: references must always be valid, so the compiler refuses any program in which a reference could be used after the value it points at is gone. That refusal is what keeps dangling references — pointers to memory that has been given back — out of compiled Rust code.
The four forms in this guide cover most E0597 reports in ordinary code. A reference taken to a value inside a block and used after the block closes. A reference to an inner-scope value stored in a variable declared outside the block, so the storage outlives the referent. A struct that holds a reference in a field, where the struct is used after the referent drops. And a closure that captures a value by reference and is called after that value's scope ends. In each family the failing program is small, the full compiler output is shown, and the fix keeps the intent of the code while making the referent live at least as long as every use of the borrow.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: the referent must outlive every use of the borrow
Every value is dropped at the end of the scope that owns it, unless ownership moves out. A reference to that value is therefore only sound inside the window between the value's creation and its drop. E0597 is the compiler reporting that a use of the reference falls outside that window: the borrow is created legally, but something — a variable, a struct, a closure, a container — carries it past the closing brace where its referent is dropped.
That is why the error is about lifetimes rather than about the borrow itself. Taking &x is never the mistake on its own; the mistake is the mismatch between two spans, the span the referent actually lives for and the span across which the borrow is used. Every fix in this guide works on one of those two spans: lengthen the referent's span by binding it in an outer scope, shorten the borrow's span by moving its last use inside the referent's scope, or remove the borrow from the design by passing an owned value instead.
Adding a lifetime annotation is not a fourth way to lengthen anything. An annotation describes a relationship the code must already satisfy; it does not extend a value's life. Where a struct holds a reference, the annotation is required so the relationship can be stated at all — the third family below — but the failing program there already carries the annotation and still fails, because the referent still drops before the holder is used.
How to read the diagnostic
Four labels appear in every E0597 diagnostic in this guide, and together they draw the two spans. "binding x declared here" marks where the referent was created, which fixes the scope that will drop it. "borrowed value does not live long enough" sits on the line that creates the borrow — this is the line in the error[E0597] header's position. "x dropped here while still borrowed" marks the closing brace where the referent's span ends. "borrow later used here" marks the use that falls after that drop, and it is the line that makes the program invalid rather than merely tight.
The fix follows from which span can move. If the referent's binding can move to an outer scope, its drop moves later and the borrow fits. If the later use can move inside the referent's scope, the borrow's span shrinks to fit. If neither can move — the value is created inside a function and only a reference escapes it — the design is asking for a reference to something that will not exist, and the repair is to return or store an owned value instead.
The first failure: a reference used after its block ends
This is the classic shape, the one the Book itself uses to introduce dangling references. The reference r is declared outside the block so it can be assigned inside it, but the value it points at, x, belongs to the block and is dropped at its closing brace. The print after the block is a use of a borrow whose referent no longer exists:
fn main() {
let r;
{
let x = 5;
r = &x;
}
println!("{r}");
}
$ rustc --edition 2021 fail_block.rs -o fail_block
error[E0597]: `x` does not live long enough
--> fail_block.rs:5:13
|
4 | let x = 5;
| - binding `x` declared here
5 | r = &x;
| ^^ borrowed value does not live long enough
6 | }
| - `x` dropped here while still borrowed
7 | println!("{r}");
| - borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0597`.
Nothing about the print is wrong in intent — the program wants the value 5 outside the block. The cheapest honest fix lengthens the referent's span: bind x in the outer scope, before r is taken, so x is dropped at the end of main instead of at the block's end, after the borrow's last use:
fn main() {
let x = 5;
let r = &x;
println!("{r}");
}
$ rustc --edition 2021 fix_block.rs -o fix_block
$ ./fix_block
5
The output is the value the failing program was trying to print. When the block exists for other reasons — it computes several values, or it scopes a temporary resource — the symmetrical fix shortens the borrow instead: move the use of r inside the block, before the closing brace, so the borrow ends while x is still alive. Which side moves depends on where the value is genuinely needed; what cannot stay is a use on the far side of the drop.
The second failure: a reference stored in a variable that outlives it
Declaring the borrowing variable first and assigning it inside a block is the same mismatch with the storage made explicit. Here r starts out pointing at placeholder, is reassigned to &x inside the block, and is printed both inside the block and after it. The inside print is fine; the outside print is the use the diagnostic names, because x was dropped when the block closed while r still claims to point at it:
fn main() {
let placeholder = 0;
let mut r = &placeholder;
println!("before: {r}");
{
let x = 10;
r = &x;
println!("inside: {r}");
}
println!("outside: {r}");
}
$ rustc --edition 2021 fail_store.rs -o fail_store
error[E0597]: `x` does not live long enough
--> fail_store.rs:7:13
|
6 | let x = 10;
| - binding `x` declared here
7 | r = &x;
| ^^ borrowed value does not live long enough
8 | println!("inside: {r}");
9 | }
| - `x` dropped here while still borrowed
10 | println!("outside: {r}");
| - borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0597`.
The assignment line is where the header points, but the defect is the pairing of spans: storage declared in the outer scope, referent created in the inner one. The fix pairs them up — x is bound in the outer scope, next to the variable that will hold its reference, so the reassignment borrows a value that lives until the end of main:
fn main() {
let placeholder = 0;
let mut r = &placeholder;
println!("before: {r}");
let x = 10;
r = &x;
println!("inside: {r}");
println!("outside: {r}");
}
$ rustc --edition 2021 fix_store.rs -o fix_store
$ ./fix_store
before: 0
inside: 10
outside: 10
All three prints now read live values: the placeholder before the reassignment, and 10 twice after it. This family is also where the owned-value fix belongs in reach order. When the inner value is produced by a function rather than by a block — the function computes something and the caller wants to keep it — no scope move is available, because the function's locals all drop when it returns. Returning the value itself, owned, instead of a reference to it is the repair: the caller then owns a value with its own full lifetime, and there is no borrow left to outlive anything.
The third failure: a struct field whose referent drops first
A struct that holds a reference states the borrow in its type. The Rust Book chapter on lifetime syntax covers this case directly: a struct holding a reference needs a lifetime annotation on that reference, and the annotation means an instance of the struct cannot outlive the referent. Holder<'a> below carries that annotation, and the program still fails — the holder h is used after the block, but the x its field points at was created inside the block:
struct Holder<'a> {
value: &'a i32,
}
fn main() {
let h;
{
let x = 7;
h = Holder { value: &x };
}
println!("{}", h.value);
}
$ rustc --edition 2021 fail_struct.rs -o fail_struct
error[E0597]: `x` does not live long enough
--> fail_struct.rs:9:29
|
8 | let x = 7;
| - binding `x` declared here
9 | h = Holder { value: &x };
| ^^ borrowed value does not live long enough
10 | }
| - `x` dropped here while still borrowed
11 | println!("{}", h.value);
| ------- borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0597`.
The annotation did its job: it let the compiler state the requirement precisely enough to reject the program. What it cannot do is satisfy the requirement — no annotation makes x live longer. The fix is the same scope move as the first family, applied to the referent: bind x outside the block, so the holder's field points at a value that is still alive at the holder's last use:
struct Holder<'a> {
value: &'a i32,
}
fn main() {
let x = 7;
let h = Holder { value: &x };
println!("{}", h.value);
}
$ rustc --edition 2021 fix_struct.rs -o fix_struct
$ ./fix_struct
7
The printed value is the referent, read through the field after the point where the failing program dropped it. When a struct seems to attract this error repeatedly, that is usually the design signal the owned-value fix answers: store the value itself in the field instead of a reference to it, and the struct stops depending on a referent owned by some other scope. The reference form earns its keep when the referent is genuinely shared and long-lived — owned by a scope that encloses every use of the holder.
The fourth failure: a closure that captures a value past its scope
A closure that uses a variable from its surroundings captures it, and using a String only to read its length captures it by reference. Assigning that closure to a variable declared outside the block carries the borrow with it: the closure value survives the block, but the s it captured is dropped at the block's end, so calling the closure afterward would read through a dead reference:
fn main() {
let f;
{
let s = String::from("hello");
f = || s.len();
}
println!("{}", f());
}
$ rustc --edition 2021 fail_closure.rs -o fail_closure
error[E0597]: `s` does not live long enough
--> fail_closure.rs:5:16
|
4 | let s = String::from("hello");
| - binding `s` declared here
5 | f = || s.len();
| -- ^ borrowed value does not live long enough
| |
| value captured here
6 | }
| - `s` dropped here while still borrowed
7 | println!("{}", f());
| - borrow later used here
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0597`.
The diagnostic adds one label the other families do not have: "value captured here", under the closure expression, naming the capture as the point where the borrow is taken. The fix follows the same pattern — the captured value moves to the outer scope, so it is still alive when the closure is called:
fn main() {
let s = String::from("hello");
let f = || s.len();
println!("{}", f());
}
$ rustc --edition 2021 fix_closure.rs -o fix_closure
$ ./fix_closure
5
The output is the length the closure computes, 5, read from a s that outlives the call. The alternative fix belongs to this family specifically: when the closure genuinely needs to own what it uses — because it is returned, stored, or sent elsewhere — making it take ownership of the captured value with move removes the borrow entirely, at the price of the original variable being unusable afterward. Reach for move when the closure's life is the long one by design; reach for the outer binding when the value has other work to do in the enclosing scope after the closure's last call.
The fixes in reach order
- Bind the referent in the outer scope when the value can be created earlier: declare it beside the variable, struct, or closure that borrows it, so its drop falls after the borrow's last use.
- Move the use inside the scope when the borrow is the thing that can shrink: a print, a read, or a call placed before the closing brace ends the borrow while the referent is alive.
- Return or store an owned value when the referent is created inside a function or a block whose scope cannot grow: ownership gives the caller a value with its own lifetime, and no borrow remains to check.
- Add or keep a lifetime annotation where a type must state the relationship — a struct field, a function signature — while remembering it only describes the requirement; the scope or ownership fix is what satisfies it.
- Give a closure ownership of its captures with
movewhen the closure is the long-lived party by design, accepting that the captured variables are consumed. - Restructure when neither span can move and ownership cannot transfer: compute the needed result inside the referent's scope and carry the result — a number, a string, a decision — out, instead of carrying the reference out.
Traps
- Adding a lifetime annotation does not extend any value's life. The struct family above fails with its annotation in place; annotations state how long a referent must live, and the compiler then checks the scopes against that statement.
- Returning a reference to a function local does not reach E0597 in its plain form: a signature like
fn make_value() -> &i32with no lifetime parameter is rejected earlier, as E0106, missing lifetime specifier — and no lifetime written there can make the local survive the return. The owned return is the fix, not a search for the right annotation. - Deleting the later use silences the error and changes the program. The "borrow later used here" label points at a use the code needs; the repair is moving a span, not amputating the use to please the checker.
- Storing the reference somewhere longer-lived — a
Vec, a field, a variable declared earlier — moves the error rather than fixing it, because the storage is exactly what carries the borrow past the drop. The container's lifetime is the borrow's lifetime now. moveon a closure is a real fix with a real cost: the captured value is consumed, so later uses of the original variable fail. It fits closures that are returned or stored, not closures that borrow a value the enclosing scope still needs.- This error is specifically the drop side of the borrow rules — the referent ends while the borrow continues. A report about two simultaneous mutable borrows, or about moving a value that is currently borrowed, names a different span problem and takes different fixes, even when the programs look similar.
Decision summary
- The borrow is a plain reference assigned in a block and used after it: bind the referent outside the block, or move the use inside it.
- The borrow is stored in a variable declared earlier: pair the spans — bind the referent in the same scope as the storage.
- The value comes from a function and only a reference escapes: return the owned value instead.
- The borrow is a struct field: keep the lifetime annotation, and bind the referent in a scope that encloses every use of the struct — or store an owned value in the field.
- The borrow is a closure capture: bind the captured value outside the closure's defining block, or use
movewhen the closure should own it. - The diagnostic's "dropped here" brace is a scope the code controls: that brace is the lever — everything in this error is decided by which side of it the binding and the use sit on.
Checklist
- The four labels in the diagnostic read as two spans: where the referent is bound, where it drops, where the borrow is taken, and where it is used after the drop.
- The mismatch classified before editing: a block scope, stored storage, a struct field, or a closure capture.
- The fix moved a span — the binding outward or the use inward — or replaced the reference with an owned value, and no later use was deleted to silence the error.
- Lifetime annotations present where a type states the relationship, and none treated as if it lengthens a value's life.
- The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
- Fix Rust E0507 "cannot move out of ..." — Vec Index, HashMap Index, a Shared Reference, a Struct Field, and a Mutable Reference
- Fix Rust E0277 "the trait bound ... is not satisfied" — Missing Trait Impl, Missing Ord, the ? Conversion, Sized, and Clone
- Rust Lazy Statics, Measured: OnceLock vs LazyLock vs lazy_static — and the 2x Claim That Does Not Reproduce
- Fix Rust E0599 "no method named" — Import the Trait, Fix the Receiver, or Use the Current Name
Sources
- Rust, Error codes index: E0597 — the definition of the error: a value was dropped while it was still borrowed, with the struct-field example and its scope-removal fix.
- The Rust Programming Language, References and Borrowing — references must always be valid, the dangling-reference example of a function returning a reference to its own local
String, the compiler guarantee that such code does not compile, and the owned-return solution. - The Rust Programming Language, Validating References with Lifetimes — lifetime annotations in struct definitions, and the rule that a struct instance cannot outlive the referent its fields borrow.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.