Created
August 20, 2019 23:36
-
-
Save rust-play/49b8f110da73080d9a3a5f0942c8aa29 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains 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::sync::Mutex; | |
#[macro_use] | |
extern crate lazy_static; | |
#[derive(Debug)] | |
struct MyDB {} | |
lazy_static! { | |
static ref DB: Mutex<Option<MyDB>> = Mutex::new(None); | |
} | |
fn get_db() -> Option<&'static MyDB> { | |
let mtx = DB.lock().unwrap(); | |
let db = mtx.as_ref(); | |
match db { | |
None => { | |
let d = MyDB {}; | |
DB.lock().unwrap().replace(d); | |
None | |
}, | |
Some(ref d) => { | |
Some(d) | |
} | |
} | |
} | |
fn main() { | |
let db = get_db(); | |
println!("{:?}", db); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment