Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 20, 2019 23:36
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 rust-play/49b8f110da73080d9a3a5f0942c8aa29 to your computer and use it in GitHub Desktop.
Save rust-play/49b8f110da73080d9a3a5f0942c8aa29 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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