Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

How would you get a static lifetime without a static object?


Unrelated 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());`


More 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.


Box::leak() will make a &'static. You can also use lazy_static! to dynamically make a static global constant.


Box::leak for one.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: