Suppose I have a function which returns a struct that contains a reference like so:
struct Foo<'a> {
x: &'a i32,
}
fn makeFoo<'a>() -> Foo<'a> {
let x = 30;
return Foo{x: &x}
}
The problem with the above code is that x will get dropped at the end of makeFoo
which causes the struct reference to outlast it.
Is there a way to transfer the lifetime of x to calling function? In this case, the obvious solution is to redefine the struct and have it take ownership of x directly, but that isn't always possible (in my case the struct is defined in a library I don't control). What is the idiomatic way to solve this problem in rust?
Edit:
While the question is superficially similar to the linked one, it is different. The problem is one of creating variables the refs of which are returned inside of a struct, and then finding a way to tie the lifetime of the refs to the struct itself (without modifying it in this case). This may be impossible, but I would be surprised to learn that this is the case, since I imagine encountering structs which hold references to values is not uncommon in third party libraries.
For example, one would think that the following might be sufficient:
struct Foo<'a> {
x: &'a i32,
}
fn makeFoo<'a>() -> (Foo<'a>, i32) {
let x = 30;
return (Foo{x: &x}, x)
}
if the compiler were clever enough to establish that the value underlying the reference &x is transferred to the caller upon return (though this does not appear to be the case).