我如何在没有赋值的情况下声明静态可变变量?

问题描述 投票:8回答:2

我尝试了以下内容

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-可能吗?如何?

static global rust declaration
2个回答
8
投票

声明时必须分配静态或常量;此后再也无法分配给它们。

静态必须纯粹是文字;它不能有任何函数调用。

目前,常数必须是纯粹的文字,但是当实现RFC 911, const fn时,将可以做更多像您想要的事情。

在函数内部,可以像在函数外部一样具有staticconst项,并且没有区别-将项(特征和类型定义,函数,&c。放在函数内部)纯粹将它们隐藏在外部范围之外。因此,您通常也可以使用let foo


7
投票

您可以在首次访问时使用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安全保证,但是对于速度比较端口,它就足够了。

© www.soinside.com 2019 - 2024. All rights reserved.