Skip to content

Instantly share code, notes, and snippets.

@eevee
Created July 13, 2013 07:50
Show Gist options
  • Save eevee/5989881 to your computer and use it in GitHub Desktop.
Save eevee/5989881 to your computer and use it in GitHub Desktop.
structs containing borrowed pointers
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();
}
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