As a general rule of reference (no pun intended), if it’s behind a memory structure of some sort (like a reference, or a smart pointer) but it has no lifetimes attached, or a
'static lifetime, then, when there is no unsafe code to possibly mangle lifetimes, you can safely assume that it is living somewhere that is not on the stack. Another thing to note, is that if there is simply no lifetime attached, it can be considered “owned”, and whether it is on the stack of the heap is dependent on the structure itself and it’s internal implementation (With the most basic example being
What does this mean?
Your both of your examples are correct, but there is a bit of an asterisk to attach, if you’ve got a
&'a T, meaning a reference to a
T where the underlying value lives for as long or longer than
'a, then you usually can’t know if the
T is in the stack or heap without interacting with the
T (eg, adding a marker when it’s created).
Some of my statements are a bit convoluted, or you might not have touched on them yet, so please let me know if you need some clarification.