Skip to content

Instantly share code, notes, and snippets.

@luqmana
Created July 27, 2013 09:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luqmana/6094308 to your computer and use it in GitHub Desktop.
Save luqmana/6094308 to your computer and use it in GitHub Desktop.
struct Parent;
impl Parent {
fn new_child<'a>(&'a self) -> Child<'a> {
Child {
parent: self
}
}
}
struct Child<'self> {
parent: &'self Parent
}
impl<'self> Child<'self> {
fn foo(&self) {}
}
fn main() {
let child;
{
let parent = Parent;
child = parent.new_child();
}
child.foo();
}
-> % rustc test.rs
test.rs:22:16: 22:22 error: borrowed value does not live long enough
test.rs:22 child = parent.new_child();
^~~~~~
test.rs:18:10: 26:1 note: borrowed pointer must be valid for the block at 18:10...
test.rs:18 fn main() {
test.rs:19 let child;
test.rs:20 {
test.rs:21 let parent = Parent;
test.rs:22 child = parent.new_child();
test.rs:23 }
...
test.rs:20:4: 23:5 note: ...but borrowed value is only valid for the block at 20:4
test.rs:20 {
test.rs:21 let parent = Parent;
test.rs:22 child = parent.new_child();
test.rs:23 }
error: aborting due to previous error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment