What is the best way to create and use a struct with only one instantiation in the system? Yes, this is necessary, it is the OpenGL subsystem, and making multiple copies of this and passing it around everywhere would add confusion, rather than relieve it.
The singleton needs to be as efficient as possible. It doesn't seem possible to store an arbitrary object on the static area, as it contains a Vec
with a destructor. The second option is to store an (unsafe) pointer on the static area, pointing to a heap allocated singleton. What is the most convenient and safest way to do this, while keeping syntax terse.
Best Solution
Non-answer answer
Avoid global state in general. Instead, construct the object somewhere early (perhaps in
main
), then pass mutable references to that object into the places that need it. This will usually make your code easier to reason about and doesn't require as much bending over backwards.Look hard at yourself in the mirror before deciding that you want global mutable variables. There are rare cases where it's useful, so that's why it's worth knowing how to do.
Still want to make one...?
Tips
In the 3 following solutions:
Mutex
then you have a global singleton without any mutability.RwLock
instead of aMutex
to allow multiple concurrent readers.Using
lazy-static
The lazy-static crate can take away some of the drudgery of manually creating a singleton. Here is a global mutable vector:
Using
once_cell
The once_cell crate can take away some of the drudgery of manually creating a singleton. Here is a global mutable vector:
Using
std::sync::SyncLazy
The standard library is in the process of adding
once_cell
's functionality, currently calledSyncLazy
:A special case: atomics
If you only need to track an integer value, you can directly use an atomic:
Manual, dependency-free implementation
There are several existing implementation of statics, such as the Rust 1.0 implementation of
stdin
. This is the same idea adapted to modern Rust, such as the use ofMaybeUninit
to avoid allocations and unnecessary indirection. You should also look at the modern implementation ofio::Lazy
. I've commented inline with what each line does.This prints out:
This code compiles with Rust 1.55.0.
All of this work is what lazy-static or once_cell do for you.
The meaning of "global"
Please note that you can still use normal Rust scoping and module-level privacy to control access to a
static
orlazy_static
variable. This means that you can declare it in a module or even inside of a function and it won't be accessible outside of that module / function. This is good for controlling access:However, the variable is still global in that there's one instance of it that exists across the entire program.