Here's a snippet:
struct T {}
fn test<'a>() -> (T, &'a T) {
let t = T{};
(t, &t)
}
The compiler tells me the the value t
doesn't live long enough, which is not quite true, since the hypothetical caller will own the value, so it's reasonable for him to also get a reference:
let (value, reference) = test(); // The value is alive here, so it's perfectly fine to reference it at this point.
Is there any way to achieve such a behavior? If not, are there any possible workarounds, besides getting a reference on a separate line like so?
let value = test();
let reference = &value;
My actual case is not about a reference, but rather about a marker value that I want not to outlive the returned T
, but the principle is the same. And I want the user to only be able to get a single marker, so a method on T
is not quite what I want here. Note how my values are immutable: I understand that a reference will cause aliasing and this question is NOT about that.