作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在此代码中,A
不必为static mut
,但是编译器强制B
为static mut
:
use std::collections::HashMap;
use std::iter::FromIterator;
static A: [u32; 21] = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
];
static mut B: Option<HashMap<u32, String>> = None;
fn init_tables() {
let hm = HashMap::<u32, String>::from_iter(A.iter().map(|&i| (i, (i + 10u32).to_string())));
unsafe {
B = Some(hm);
}
}
fn main() {
init_tables();
println!("{:?} len: {}", A, A.len());
unsafe {
println!("{:?}", B);
}
}
HashMap
将由多个函数使用,而不会用
unsafe
块乱丢我的所有代码。
rustc
而不是
cargo
,因此我不希望像
lazy_static
这样的外部包装箱获得“帮助”。我试图解密该程序包中的宏的作用,但是一直没有结束。
thread_local()
和
RefCell
编写此代码,但是在使用
A
初始化该版本的
B
时遇到了麻烦。
B
(不使用
init_tables()
之类的功能),那么您的答案可能是正确的。
init_tables()
这样的函数是不可避免的,是否有像访问器函数这样的技巧可以减少程序中的
unsafe
垃圾呢?
最佳答案
How to get stuff into the initvars section of a program in Rust?
rustc
将
static
数据放入生成的二进制文件的
.rodata
节中,并将
static mut
数据放入
.data
节中:
#[no_mangle]
static DATA: std::ops::Range<u32> = 0..20;
fn main() { DATA.len(); }
$ rustc static.rs
$ objdump -t -j .rodata static
static: file format elf64-x86-64
SYMBOL TABLE:
0000000000025000 l d .rodata 0000000000000000 .rodata
0000000000025490 l O .rodata 0000000000000039 str.0
0000000000026a70 l O .rodata 0000000000000400 elf_crc32.crc32_table
0000000000026870 l O .rodata 0000000000000200 elf_zlib_default_dist_table
0000000000026590 l O .rodata 00000000000002e0 elf_zlib_default_table
0000000000025060 g O .rodata 0000000000000008 DATA
0000000000027f2c g O .rodata 0000000000000100 _ZN4core3str15UTF8_CHAR_WIDTH17h6f9f810be98aa5f2E
static mut
更改为
static
会极大地更改生成的二进制文件。
.rodata
节是只读的,尝试对其进行写操作将使程序故障。
If init_tables() is of the judgement day category (inevitable)
.rodata
链接无效,因此必须直接对其进行控制:
use std::collections::HashMap;
use std::iter::FromIterator;
static A: std::ops::Range<u32> = 0..20;
#[link_section = ".bss"]
static B: Option<HashMap<u32, String>> = None;
fn init_tables() {
let data = HashMap::from_iter(A.clone().map(|i| (i, (i + 10).to_string())));
unsafe {
let b: *mut Option<HashMap<u32, String>> = &B as *const _ as *mut _;
(&mut *b).replace(data);
}
}
fn main() {
init_tables();
println!("{:?} len: {}", A, A.len());
println!("{:#?} 5 => {:?}", B, B.as_ref().unwrap().get(&5));
}
I don't want the "help" of extern crates like lazy_static
lazy_static
并不那么复杂。它对
Deref
特性有一些巧妙的用法。这是一个经过简化的独立版本,比第一个示例更符合人体工程学:
use std::collections::HashMap;
use std::iter::FromIterator;
use std::ops::Deref;
use std::sync::Once;
static A: std::ops::Range<u32> = 0..20;
static B: BImpl = BImpl;
struct BImpl;
impl Deref for BImpl {
type Target = HashMap<u32, String>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
static LAZY: (Option<HashMap<u32, String>>, Once) = (None, Once::new());
LAZY.1.call_once(|| unsafe {
let x: *mut Option<Self::Target> = &LAZY.0 as *const _ as *mut _;
(&mut *x).replace(init_tables());
});
LAZY.0.as_ref().unwrap()
}
}
fn init_tables() -> HashMap<u32, String> {
HashMap::from_iter(A.clone().map(|i| (i, (i + 10).to_string())))
}
fn main() {
println!("{:?} len: {}", A, A.len());
println!("{:#?} 5 => {:?}", *B, B.get(&5));
}
关于rust - 有没有办法在不使它成为静态mut的情况下初始化一个非平凡的静态std::collections::HashMap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59551404/
我是一名优秀的程序员,十分优秀!