[][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.

This type uses the blocking synchronization mechanism provided by the underlying operating system.

For the API of this type alias, see the API of the generic Lazy type.


use std::sync::Mutex;

use conquer_once::Lazy;

static MUTEX: Lazy<Mutex<Vec<i32>>> = Lazy::new(Mutex::default);

let mut lock = MUTEX.lock().unwrap();


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");

assert_eq!(CAPITALS.get(&"Norway"), Some(&"Oslo"));
assert_eq!(CAPITALS.get(&"Belgium"), Some(&"Brussels"));
assert_eq!(CAPITALS.get(&"Latvia"), Some(&"Riga"));