I followed the suggested solution provided by the compiler but it resulted in the same error. I'm thinking that lazy_static is not a good use for global variables.
extern crate lazy_static;
#[macro_use]
use std::collections::HashMap;
use lazy_static::lazy_static;
use std::sync::Mutex;
#[derive(Debug)]
pub struct Service {
url: String,
used: i32,
}
#[derive(Debug)]
pub struct Protocol {
name: String,
services: Vec<Service>,
}
#[derive(Debug)]
struct Protocols {
map: HashMap<String, Option<Protocol>>,
}
lazy_static! {
static ref HASHMAP: Mutex<HashMap<String, &'static Option<Protocol>>> = {
let m = HashMap::new();
Mutex::new(m)
};
}
fn main() {
let mut map = HASHMAP.lock().unwrap();
let p = Protocol {
name: "One".to_string(),
services: Vec::<Service>::new(),
};
let mut map = HASHMAP.lock().unwrap();
let binding = Some(p); // suggested by compiler.
map.insert("One3".to_string(), &binding);
println!("{:?}", HASHMAP.lock().unwrap());
}