如何在没有赋值的情况下声明静态可变变量? [英] How do I declare a static mutable variable without assignment?
问题描述
我尝试了以下操作
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-有可能吗?如何?
Q1,Q2,Q3 - Is it possible and how?
推荐答案
声明时必须分配静态或常量;
A static or constant must be assigned when declared; they can never be assigned to after that.
静态内容必须是纯文字;它不能有任何函数调用。
A static must be purely literals; it cannot have any function calls.
当前常量必须纯粹是文字,但是当 RFC 911,const fn 已实现,将有可能做更多像您想要的事情。
A constant must at present be purely literals, but when RFC 911, const fn is implemented it will be possible to do things more like how you desire.
在函数内部,您可以拥有 static
或 const
项就像在函数外部一样,并且没有区别-在函数内部放置项目(特征和类型定义,函数,& c。)纯粹是将它们隐藏在外部范围之外。因此,您通常也可以使用 let foo
。
Inside a function, you can have static
or const
items, just as outside a function, and there is no difference—placing items (trait and type definitions, functions, &c.) inside a function purely hides them from the outside scope. Therefore you typically might as well use let foo
.
这篇关于如何在没有赋值的情况下声明静态可变变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!