Constants
Declare lazily evaluated constant
Declares a lazily evaluated constant HashMap
. The HashMap
will
be evaluated once and stored behind a global static reference.
use lazy_static::lazy_static; use std::collections::HashMap; lazy_static! { static ref PRIVILEGES: HashMap<&'static str, Vec<&'static str>> = { let mut map = HashMap::new(); map.insert("James", vec!["user", "admin"]); map.insert("Jim", vec!["user"]); map }; } fn show_access(name: &str) { let access = PRIVILEGES.get(name); println!("{}: {:?}", name, access); } fn main() { let access = PRIVILEGES.get("James"); println!("James: {:?}", access); show_access("Jim"); }
Std:cell
OnceCell
is included in the standard library as an alternative.
#![allow(unused)] fn main() { use std::cell::OnceCell; let cell = OnceCell::new(); assert!(cell.get().is_none()); let value: &String = cell.get_or_init(|| { "Hello, World!".to_string() }); assert_eq!(value, "Hello, World!"); assert!(cell.get().is_some()); }