Home › Rust

error[E0597]: `x` does not live long enough (Rust) — how to fix

error[E0597]: `x` does not live long enough
 --> src/main.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

Quick fix: Make the owner live at least as long as the reference: declare the value in the outer scope, store an owned value (String, Vec<T>, .clone(), .to_owned()) instead of a reference, or share it with Rc/Arc. For threads, use std::thread::scope or move owned data into the closure.

What it means

Every value is dropped at the end of the scope that owns it. A reference &x is only valid while x exists. E0597 means the compiler found a reference that's still needed after the point where x is dropped. The three labels tell the story: where the borrow happens, where x is dropped (dropped here while still borrowed), and where the borrow is used later.

let r;
{
    let x = 5;
    r = &x;
}               // x dropped here
println!("{r}"); // r would point to freed memory

Cause 1: the value lives in an inner scope

The owner is declared inside a block, loop body or match arm, but the reference is used outside.

Fix 1

Move the owner out so it lives as long as the reference:

let x = 5;
let r;
{
    r = &x;
}
println!("{r}");

Or don't keep a reference at all: copy or move the value out of the block.

let r = {
    let x = 5;
    x          // moved/copied out
};

Cause 2: storing references to loop-local data in a collection

let mut names: Vec<&str> = Vec::new();
for line in input.lines() {
    let name = line.trim().to_uppercase();   // new String each iteration
    names.push(&name);                        // E0597: name dropped at end of iteration
}

Fix 2

Store owned values:

let mut names: Vec<String> = Vec::new();
for line in input.lines() {
    names.push(line.trim().to_uppercase());
}

Storing &str works only when the data is owned somewhere that outlives the vector (here input), e.g. names.push(line.trim()).

Cause 3: a 'static requirement

Some APIs require data that lives for the whole program: std::thread::spawn, async task spawners such as tokio::spawn, or values put in a static. Borrowing a local for them fails:

let s = String::from("hi");
let r: &'static str = &s;     // E0597: s doesn't live forever

Fix 3

Give them owned data, moved in:

let data = vec![1, 2, 3];
let handle = std::thread::spawn(move || {
    println!("{data:?}");
});
handle.join().unwrap();

If several threads need the same data, wrap it in Arc and clone the Arc:

use std::sync::Arc;
let data = Arc::new(vec![1, 2, 3]);
let d = Arc::clone(&data);
std::thread::spawn(move || println!("{d:?}"));

If you only need the threads during a block, std::thread::scope (Rust 1.63+) lets them borrow locals, because all scoped threads are joined before the scope ends:

let data = vec![1, 2, 3];
std::thread::scope(|s| {
    s.spawn(|| println!("{data:?}"));
});

Cause 4: a struct holding a reference outlives its source

struct Parser<'a> { input: &'a str }

let parser;
{
    let text = std::fs::read_to_string("a.txt")?;
    parser = Parser { input: &text };
}
// parser used here: E0597

Fix 4

Keep text alive in the outer scope, or make the struct own its data (input: String) if it needs to live independently.

Last updated 2026-11-30.