Initialize an immutable global variable without locks
⚓ Rust 📅 2025-11-22 👤 surdeus 👁️ 10Hi! I'm trying to create a global immutable config in rust. In C++, I'd typically write something like:
const Config config = load_config();
But in Rust, the compiler tells me I can't use a non-const function to initialize static variables.
I find some solutions like using LazyLock and OnceLock, but they checks whether the variable is initialized everytime I read the variable.
In my case, I'm initializing at the start of the program before any new thread is created, so these runtime checks feel unnecessary. What is the best practice in this case? Thanks advanced.
3 posts - 3 participants
🏷️ Rust_feed