-
I wanna create a async HashMap singleton, So any where can use it in my project.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The shared state chapter in the Tokio tutorial covers this. As for making it a global variable, you can do this with use std::sync::Mutex;
use std::collections::HashMap;
lazy_static! {
static ref MY_SINGLETON: Mutex<HashMap<String, String>> = Mutex::new(HashMap::new());
} Note that when using Note also that you should not be using an |
Beta Was this translation helpful? Give feedback.
The shared state chapter in the Tokio tutorial covers this. As for making it a global variable, you can do this with
lazy_static!
.Note that when using
lazy_static!
, you do not need anArc
. The purpose of anArc
is to share the variable, but we already get this fromlazy_static!
.Note also that you should not be using an
tokio::sync
lock for this, but rather anstd::sync
lock. Please read the shared state chapter that I linked for an explanation of why. As forRwLock
vsMutex
, you should prefer aMutex
, and if that doesn't do the …