Skip to content

Latest commit

 

History

History
35 lines (26 loc) · 940 Bytes

lazy-constant.zh.md

File metadata and controls

35 lines (26 loc) · 940 Bytes

声明,延迟计算的常量

[![lazy_static-badge]][lazy_static] [![cat-caching-badge]][cat-caching] [![cat-rust-patterns-badge]][cat-rust-patterns]

声明一个延迟计算的HashMap常量。 这个HashMap会执行一次,在之后,会存储在全局的静态引用(PRIVILEGES)背后。

#[macro_use]
extern crate 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");
}