I'm a beginner translating a familiar Cpp project to Rust. The project contains a class called Globals which stores global config parameters. Here is an extract from its cpp file:
static Globals &__get()
{
static Globals globals;
return globals;
}
const Globals &Globals::get()
{
auto &globals = __get();
if (!globals.initialized) {
throw std::runtime_error("Initialize globals first");
}
return globals;
}
void Globals::set(const Globals &globals)
{
__get() = globals;
}
How would I translate this to Rust? From what I can tell __get() implements some kind of singleton logic. I've read about the lazy_static crate to achieve something similar, but unlocking the variable each time I want to read its value seems to be too verbose. Isn't it possible to achieve this using an interface like Globals::get() in the cpp code.
I rarely post, so if I forgot something, just tell me and I'll provide details. Thanks!
setmakes your global mutable. It may only be temporarily mutable e.g. you set it once at the start of the program, but Rust's got no way to know this. Also while the first link covers mutable globals, it also covers (somewhat implicitly) the ability to set-once then just access: just don't use the mutex (this applies to both lazy_static and once_cell).lazy_static. I maybe overcomplicated it a bit by using anOptioninstead of having a flag insideGlobalsthat indicates initialized-ness, because that means you can't return anRwLockReadGuardfromget()directly, so I had to wrap it myself using some of the ideas in How do I return a reference to something inside a RefCell without breaking encapsulation?.