Skip to content

Instantly share code, notes, and snippets.

@kellpossible
Created September 27, 2016 08:04
Show Gist options
  • Save kellpossible/082d1979beb9b0ffcc1fc4e41cd9c8f1 to your computer and use it in GitHub Desktop.
Save kellpossible/082d1979beb9b0ffcc1fc4e41cd9c8f1 to your computer and use it in GitHub Desktop.
struct A<'a> {
s: &'a String
}
struct B<'a> {
a: A<'a>,
s: String
}
impl<'a> B<'a> {
fn new() -> B<'a> {
let new_s = String::from("some string");
let new_a = A {s: &new_s};
return B {a: new_a, s: new_s};
}
}
@kellpossible
Copy link
Author

Error I'm getting (ignore the line numbers!)

error: `new_s` does not live long enough
  --> main.rs:18:28
   |
18 |         let new_a = A {s: &new_s};
   |                            ^^^^^ does not live long enough
19 |         return B {a: new_a, s: new_s};
20 |     }
   |     - borrowed value only lives until here
   |
note: borrowed value must be valid for the lifetime 'a as defined on the block at 16:22...
  --> main.rs:16:23
   |
16 |     fn new() -> B<'a> {
   |                       ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment