Skip to content

Instantly share code, notes, and snippets.

@xevix
Created March 2, 2016 08:36
Show Gist options
  • Save xevix/fcca47f5fc3868bb5ed0 to your computer and use it in GitHub Desktop.
Save xevix/fcca47f5fc3868bb5ed0 to your computer and use it in GitHub Desktop.
Rust Lifetimes
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