Skip to content

Instantly share code, notes, and snippets.

@KodrAus
Last active August 22, 2016 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KodrAus/906155dc5884c63988309abb58153beb to your computer and use it in GitHub Desktop.
Save KodrAus/906155dc5884c63988309abb58153beb to your computer and use it in GitHub Desktop.
Rust Lifetimes
//Lifetimes are generic parameters
//'a and 'b are lifetimes
//'static is a special lifetime for static memory
struct Person<'a> {
pub id: i32,
//Question: can we mutate parent.unwrap().id?
pub parent: Option<&'a Person<'a>>
}
fn main() {
let parent = Person {
id: 1,
parent: None
};
let child = Person {
id: 2,
parent: Some(&parent)
};
println!("Parent: {}", child.parent.unwrap().id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment