在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论