Created
July 13, 2013 07:50
-
-
Save eevee/5989881 to your computer and use it in GitHub Desktop.
structs containing borrowed pointers
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 Parent { | |
data: uint, | |
} | |
struct Child<'self> { | |
parent: &'self Parent, | |
} | |
impl<'self> Parent { | |
fn new() ⟶ Parent { | |
return Parent{ data: 0 }; | |
} | |
fn spawn_child(&'self self) ⟶ &'self Child { | |
return &Child{ parent: self }; | |
} | |
} | |
fn main() { | |
let parent = Parent::new(); | |
let child = parent.spawn_child(); | |
} |
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
lifetimes.rs:15:31: 15:37 error: cannot infer an appropriate lifetime due to conflicting requirements | |
lifetimes.rs:15 return &Child{ parent: self }; | |
^~~~~~ | |
lifetimes.rs:14:48: 16:5 note: first, the lifetime cannot outlive the lifetime &'self as defined on the block at 14:48... | |
lifetimes.rs:14 fn spawn_child(&'self self) -> &'self Child { | |
lifetimes.rs:15 return &Child{ parent: self }; | |
lifetimes.rs:16 } | |
lifetimes.rs:15:31: 15:37 note: ...due to the following expression | |
lifetimes.rs:15 return &Child{ parent: self }; | |
^~~~~~ | |
lifetimes.rs:14:48: 16:5 note: but, the lifetime must be valid for the anonymous lifetime #1 defined on the block at 14:48... | |
lifetimes.rs:14 fn spawn_child(&'self self) -> &'self Child { | |
lifetimes.rs:15 return &Child{ parent: self }; | |
lifetimes.rs:16 } | |
lifetimes.rs:15:15: 15:21 note: ...due to the following expression | |
lifetimes.rs:15 return &Child{ parent: self }; | |
^~~~~~ | |
error: aborting due to previous error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment