logoalt Hacker News

FpUsertoday at 12:44 PM4 repliesview on HN

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.


Replies

kd5bjotoday at 12:59 PM

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.

show 1 reply
mplanchardtoday at 2:32 PM

You can Box::leak() it to make a &’static ref to it.

LtdJorgetoday at 12:57 PM

Use a LazyLock for your state, then