Skip to content

Instantly share code, notes, and snippets.

@wackywendell
Last active August 29, 2015 14:02
Show Gist options
  • Save wackywendell/4a1e2c5190480e3bef10 to your computer and use it in GitHub Desktop.
Save wackywendell/4a1e2c5190480e3bef10 to your computer and use it in GitHub Desktop.
Fails to compile, with a complaint about "foo does not live long enough... reference must be valid for the static lifetime"
struct Foo {
n : int
}
struct Bar<'a> {
n : int,
other : Option<&'a mut Foo>
}
impl<'a> Bar<'a> {
fn set_foo(&mut self, foo : &'a mut Foo){
self.other = Some(foo);
}
}
fn with_bar(func : |&mut Bar|) {
let mut bar = Bar { n : 1, other : None };
func(&mut bar)
}
fn main() {
let mut foo = Foo { n : 4 };
with_bar(|bar: &mut Bar| {
bar.set_foo(&mut foo);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment