Cargo.toml

[dependencies]
lazy_static = "1.4.0"

main.rs

#[macro_use]
extern crate lazy_static;
use std::collections::HashMap;

lazy_static! {
static ref HASHMAP: HashMap<u32, &'static str> = {
let mut m = HashMap::new();
m.insert(0, "foo");
m.insert(1, "bar");
m.insert(2, "baz");
m
};
static ref COUNT: usize = HASHMAP.len();
static ref NUMBER: u32 = times_two(21);
} fn times_two(n: u32) -> u32 { n * 2 } pub fn test() {
println!("The map has {} entries.", *COUNT);
println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap());
println!("A expensive calculation on a static results in: {}.", *NUMBER);
}

注意代码static前有一个单引号,只有一个单引号,没有成对;并不是写错了,而是rust的语法就是如此,运行上面代码输出

The map has 3 entries.
The entry for `0` is "foo".
A expensive calculation on a static results in: 42.

最新文章

  1. C++随笔:.NET CoreCLR之GC探索(3)
  2. SqlServer数据冗余的问题和解决
  3. [.net 面向对象程序设计进阶] (8) 托管与非托管
  4. Android驱动入门-Led控制+app+ndk库+底层驱动
  5. windows定时执行百度新闻爬虫
  6. [aspx]控件及代码小例
  7. android: startActivityForResult用法详解
  8. Linux_DHCP服务搭建
  9. this 关键字
  10. ASP.NET中常用方法
  11. GUI树组件,表格
  12. Linux C判断日期格式是否合法
  13. mvn 一些操作
  14. 大数据计算:如何仅用1.5KB内存为十亿对象计数
  15. 改动导航栏上返回button上的字,比如把back改动为返回
  16. DevExpress XtraReports 入门一 创建 Hello World 报表
  17. SVN下错误集锦
  18. 【Android Developers Training】 8. 定义Action Bar风格
  19. php 数组对象之间的转换
  20. 高通开发笔记---yukon worknote

热门文章

  1. Go语言核心36讲(Go语言进阶技术十六)--学习笔记
  2. 『学了就忘』Linux基础命令 — 35、网络中与其他机器通信的命令
  3. 一个反直觉的sql
  4. 解决异常:“The last packet sent successfully to the server was 0 milliseconds ago. ”的办法
  5. ant命令
  6. 强化学习之MountainCarContinuous(注册自己的gym环境)
  7. CF1418G Three Occurrences
  8. NOI2021 去不了记
  9. distmat 计算遗传距离
  10. kubernetes部署 kube-apiserver服务