Skip to content

Instantly share code, notes, and snippets.

@aurexav
Last active April 8, 2020 06:14
Show Gist options
  • Save aurexav/c485997bc0f9b7a5abae1ff67378f8fc to your computer and use it in GitHub Desktop.
Save aurexav/c485997bc0f9b7a5abae1ff67378f8fc to your computer and use it in GitHub Desktop.
[Rust] Confused About Lifetimes
struct Test {
x: i32
};
let a;
{
let b = &Test { x: 42 }; // Temporary struct Test put on the stack
a = b;
} // Temporary Test dropped here?
println!("{}", a.x); // Temporary Test used here?
@aurexav
Copy link
Author

aurexav commented Oct 29, 2019


Alice:
What happened here is the same that happens to a literal string. The constant is hard coded somewhere in the binary and the reference is a 'static reference that points into the executable.


More detail: https://users.rust-lang.org/t/confused-about-lifetimes/34000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment