Shared mutable state in Rust (2022)
16 points by vinhnx 4 days ago | 3 comments
FpUser 2 hours ago
I have shared mutable state which is available through the whole application lifetime. Having ARC in this situation makes no sense, single mutex should be all I need.
replykd5bjo 2 hours ago
That's also an option available to you: Mutex::new() is const, so there's no trouble putting one in a static variable. If the inner value can't be const-constructed, you'll need a way to prevent access before it's initialized; for that, you can use a OnceLock or just Box::leak() the Mutex once it's constructed and pass around a simple reference.
replyLtdJorge 2 hours ago
Use a LazyLock for your state, then
reply