fail-parent-child.rs
gistfile1.txt
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 |