Skip to content

Instantly share code, notes, and snippets.

@ryanmcgrath
Created September 28, 2023 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanmcgrath/8d1f0a3e97b62be8a041737d3f8c4080 to your computer and use it in GitHub Desktop.
Save ryanmcgrath/8d1f0a3e97b62be8a041737d3f8c4080 to your computer and use it in GitHub Desktop.
use std::collections::HashMap;
use std::sync::Mutex;
use lazy_static::lazy_static;
type GlobalMap = Mutex<HashMap<&'static str, &'static str>>;
lazy_static! {
static ref GLOBAL_DATA2: GlobalMap = Mutex::new(HashMap::new());
}
struct GlobalData2;
impl GlobalData2 {
pub fn set(key: &'static str, value: &'static str) {
let mut map = GLOBAL_DATA2.lock().unwrap();
map.insert(key, value);
}
pub fn get(key: &str) -> &'static str {
let map = GLOBAL_DATA2.lock().unwrap();
map.get(key)
.expect("Unable to fetch key?")
}
}
fn main() {
GlobalData2::set("Hello", "World");
let s = GlobalData2::get("Hello");
println!("{s}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment