4 ms·
> creating a &'static in some initialization code that lives for the rest of the program is a rather common use case Isn't that the use-case for lazy_static? O
by alpaca128 4y ago
> creating a &'static in some initialization code that lives for the rest of the program is a rather common use case
Isn't that the use-case for lazy_static? Or SyncOnceCell once it's in stable Rust.
Though I'm not sure how it's implemented under the hood.
- liftm 4y agoHm, good point. (It's implemented as a Once (which is an atomic usize) and an UnsafeCell for holding the actual data.) And while using SyncOnceCell normally might incur some small checking overhead on each access, you can use it to obtain a &'static. https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=506632ca984ccc6732114373c535ce28 https://play.rust-lang.org/?version=nightly&mode=debug&editi... But then, by tialaramex's logic, isn't it just as bad as Box::leak? (Maybe not quite as, because it can leak maximally one thing. You can't do this in a loop.)