gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:46:02 24 4
gpt4 key购买 nike

我尝试了以下方法

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实现后,将有可能按照您的意愿做事。

在函数内部,你可以有 staticconst 项目,就像在函数外部一样,没有区别——放置项目(特征和类型定义,函数, &c.) 在一个函数内完全将它们从外部范围隐藏起来。因此,您通常也可以使用 let foo

关于static - 如何在没有赋值的情况下声明静态可变变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30145814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com