3.7 rust 静态块
2024-09-08 11:35:58
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.
最新文章
- C++随笔:.NET CoreCLR之GC探索(3)
- SqlServer数据冗余的问题和解决
- [.net 面向对象程序设计进阶] (8) 托管与非托管
- Android驱动入门-Led控制+app+ndk库+底层驱动
- windows定时执行百度新闻爬虫
- [aspx]控件及代码小例
- android: startActivityForResult用法详解
- Linux_DHCP服务搭建
- this 关键字
- ASP.NET中常用方法
- GUI树组件,表格
- Linux C判断日期格式是否合法
- mvn 一些操作
- 大数据计算:如何仅用1.5KB内存为十亿对象计数
- 改动导航栏上返回button上的字,比如把back改动为返回
- DevExpress XtraReports 入门一 创建 Hello World 报表
- SVN下错误集锦
- 【Android Developers Training】 8. 定义Action Bar风格
- php 数组对象之间的转换
- 高通开发笔记---yukon worknote
热门文章
- Go语言核心36讲(Go语言进阶技术十六)--学习笔记
- 『学了就忘』Linux基础命令 — 35、网络中与其他机器通信的命令
- 一个反直觉的sql
- 解决异常:“The last packet sent successfully to the server was 0 milliseconds ago. ”的办法
- ant命令
- 强化学习之MountainCarContinuous(注册自己的gym环境)
- CF1418G Three Occurrences
- NOI2021 去不了记
- distmat 计算遗传距离
- kubernetes部署 kube-apiserver服务