I understand that the preferred way to implement something like a global/instance/module variable in Rust is to create said variable in main()
or other common entry point and then pass it down to whoever needs it.
It also seems possible to use a lazy_static
for an immutable variable, or it can be combined with a mutex
to implement a mutable one.
In my case, I am using Rust to create a .so with bindings to Python and I need to have a large amount of mutable state stored within the Rust library (in response to many different function calls invoked by the Python application).
What is the preferred way to store that state?
Is it only via the mutable lazy_static
approach since I have no main()
(or more generally, any function which does not terminate between function calls from Python), or is there another way to do it?