Created
September 28, 2023 07:10
-
-
Save ryanmcgrath/8d1f0a3e97b62be8a041737d3f8c4080 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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