Content-Length: 24373 | pFad | https://doc.rust-lang.org/std/string/../error/../string/../../../std/../error_codes/./E0597.html

E0597 - Error codes index

Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Error code E0597

This error occurs because a value was dropped while it was still borrowed.

Erroneous code example:

#![allow(unused)]
fn main() {
struct Foo<'a> {
    x: Option<&'a u32>,
}

let mut x = Foo { x: None };
{
    let y = 0;
    x.x = Some(&y); // error: `y` does not live long enough
}
println!("{:?}", x.x);
}

Here, y is dropped at the end of the inner scope, but it is borrowed by x until the println. To fix the previous example, just remove the scope so that y isn’t dropped until after the println

#![allow(unused)]
fn main() {
struct Foo<'a> {
    x: Option<&'a u32>,
}

let mut x = Foo { x: None };

let y = 0;
x.x = Some(&y);

println!("{:?}", x.x);
}








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://doc.rust-lang.org/std/string/../error/../string/../../../std/../error_codes/./E0597.html

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy