我尝试了以下内容
struct mbuf
{
cacheline: *mut [u64], // great amount of rows follows below
// ..........
}
static mut arr: [mbuf; 32]; // Q1 my main aim
// something as before but using Vec; // Q2 also main aim
fn main() {
// let static mut arr: [mbuf; 32]; // Q3 also doesn't work
// static mut arr: [mbuf; 32]; // Q3 also doesn't work
}
出现错误
src/main.rs:74:29: 74:30 error: expected one of `+` or `=`, found `;`
src/main.rs:74 static mut arr: [mbuf; 32];
^
Q1,Q2,Q3-可能吗?如何?
声明时必须分配静态或常量;此后再也无法分配给它们。
静态必须纯粹是文字;它不能有任何函数调用。
目前,常数必须是纯粹的文字,但是当实现RFC 911, const fn时,将可以做更多像您想要的事情。
在函数内部,可以像在函数外部一样具有static
或const
项,并且没有区别-将项(特征和类型定义,函数,&c。放在函数内部)纯粹将它们隐藏在外部范围之外。因此,您通常也可以使用let foo
。
您可以在首次访问时使用lazy-static初始化静态数组,尽管它可能会产生最小的开销(似乎每次访问静态变量时都会调用Once::call_once。
例如,Cargo.toml:
[package]
name = "arr"
version = "0.0.1"
[[bin]]
name = "arr"
path = "arr.rs"
[dependencies]
lazy_static = "*"
arr.rs:
#[macro_use]
extern crate lazy_static;
use std::mem;
use std::ptr;
#[derive(Debug)]
struct Mbuf {
cacheline: *mut u64,
}
// Let's pretend it's thread-safe to appease the lazy_static! constrains.
unsafe impl Sync for Mbuf { }
lazy_static! {
static ref ARR: [Mbuf; 32] = {
let mut tmp: [Mbuf; 32] = = unsafe { mem::MaybeUninit::uninit().assume_init() };
for idx in 0..tmp.len() {
tmp[idx] = Mbuf { cacheline: ptr::null_mut() };
}
tmp
};
}
fn main() {
println!("{:?}", *ARR);
}
或者,仅使自己的惰性访问器:
use std::mem;
use std::ptr;
#[derive(Debug)]
struct Mbuf {
cacheline: *mut u64,
}
static mut ARR: Option<[Mbuf; 32]> = None;
fn arr() -> &'static mut [Mbuf; 32] {
unsafe {
if ARR.is_none() {
let mut tmp: [Mbuf; 32] = mem::MaybeUninit::uninit().assume_init();
for idx in 0..tmp.len() {
tmp[idx] = Mbuf { cacheline: ptr::null_mut() };
}
ARR = Some(tmp);
}
mem::transmute(ARR.as_mut().unwrap())
}
}
fn main() {
println!("{:?}", arr());
}
不用说,此代码不是线程安全的,因此避开了一些Rust安全保证,但是对于速度比较端口,它就足够了。