OnceLock or lazy_static for singletons.
Using OnceLock:
1use std::sync::OnceLock;23static INSTANCE: OnceLock<MyApp> = OnceLock::new();45fn get_instance() -> &'static MyApp {6 INSTANCE.get_or_init(|| MyApp::new())7}
Using once_cell:
1use once_cell::sync::Lazy;23static INSTANCE: Lazy<Mutex<MyApp>> = Lazy::new(|| {4 Mutex::new(MyApp::new())5});
Key: OnceLock for simple, Mutex+Lazy for mutable.