3 ms·
How would you get a static lifetime without a static object?
by celeritascelery 3y ago
How would you get a static lifetime without a static object?
- unshavedyak 3y agoUnrelated to the above convo, but: An owned object can be bound by a static lifetime, for example. Eg, `fn foo<T: 'static>(t: T){}\n foo(String::new());`
- vlovich123 3y agoMore dangerous / less common, but you can also launder lifetimes through unsafe where you use ‘static to indicate “it’s not possible to verify lifetime at compile time but I promise you this will outlive you”. An example of this would be storing a reference that is reference counted but you don’t want the lifetime to bleed out into the type signature.
- ben-schaaf 3y agoBox::leak for one.
- remram 3y agoBox::leak() will make a &'static. You can also use lazy_static! to dynamically make a static global constant.