Created
March 2, 2016 08:36
-
-
Save xevix/fcca47f5fc3868bb5ed0 to your computer and use it in GitHub Desktop.
Rust Lifetimes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Foo { | |
x: i32, | |
} | |
struct Bar<'a> { | |
foo: &'a Foo, | |
// Uncomment to disallow drop() | |
x: ::std::cell::RefCell<() /* Option<&'a Bar<'a>>*/>, | |
} | |
impl<'a> Bar<'a> { | |
fn method(&'a self) { /* *self.x.borrow_mut() = Some(self)*/ } | |
fn method2(&'a mut self) {} | |
} | |
fn main() { | |
let foo = Foo {x: 10}; | |
let bar = Bar { foo: &foo, x: Default::default() }; | |
bar.method(); | |
//bar.method2(); // "cannot borrow immutable local variable `bar` as mutable" | |
drop(bar); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment