[−][src]Type Definition conquer_once::Lazy
type Lazy<T, F = fn() -> T> = Lazy<T, ParkThread, F>;
A type for lazy initialization of e.g. global static variables, which
provides the same functionality as the lazy_static!
macro.
For the API of this type alias, see the API of the generic
Lazy
type.
Examples
use std::sync::Mutex; use conquer_once::Lazy; static MUTEX: Lazy<Mutex<Vec<i32>>> = Lazy::new(Mutex::default); let mut lock = MUTEX.lock().unwrap(); lock.push(1); lock.push(2); lock.push(3); assert_eq!(lock.as_slice(), &[1, 2, 3]);
The associated new
function can be used with any function or
closure that implements Fn() -> T
.
use std::collections::HashMap; use conquer_once::Lazy; static CAPITALS: Lazy<HashMap<&str, &str>> = Lazy::new(|| { let mut map = HashMap::new(); map.insert("Norway", "Oslo"); map.insert("Belgium", "Brussels"); map.insert("Latvia", "Riga"); map }); assert_eq!(CAPITALS.get(&"Norway"), Some(&"Oslo")); assert_eq!(CAPITALS.get(&"Belgium"), Some(&"Brussels")); assert_eq!(CAPITALS.get(&"Latvia"), Some(&"Riga"));