2

I am trying to return an object together with a reference to its internal body, e.g.

struct S {
    i: i8,
}

fn ret_ref() -> (S, &i8) {
    let s = S{i: 3};
    (s, &s.i)
}

And the lifetime checker recognized that &i8 might have a shorter lifetime than the original value. now if I return s together, is there a way to extend the lifetime of the reference?

xis
  • 24,330
  • 9
  • 43
  • 59
  • 3
    Your returned tuple is equivalent to a struct like in this: [Why can't I store a value and a reference to that value in the same struct?](https://stackoverflow.com/questions/32300132/why-cant-i-store-a-value-and-a-reference-to-that-value-in-the-same-struct) – cafce25 Dec 20 '22 at 22:20

0 Answers0