定义静态向量

时间:2016-06-25 21:22:54

标签: vector static global-variables rust

我正在尝试使用我自己设计的结构定义一个静态Vec,以便在我的程序中全局使用(是的,它确实需要是可变的),但是我得到了两个错误之一。当给定构造函数时,编译器会抱怨并且不编译:

static mut stamps: Vec<StampedData> = Vec::new();
error: mutable statics are not allowed to have destructors [E0397]
static mut stamps: Vec<StampedData> = Vec::new();
                                      ^~~~~~~~~~
error: statics are not allowed to have destructors [E0493]
static mut stamps: Vec<StampedData> = Vec::new();
                                      ^~~~~~~~~~
error: function calls in statics are limited to struct and enum constructors [E0015]
static mut stamps: Vec<StampedData> = Vec::new();
                                      ^~~~~~~~~~
note: a limited form of compile-time function evaluation is available on a nightly compiler via `const fn`
static mut stamps: Vec<StampedData> = Vec::new();
                                      ^~~~~~~~~~

当我省略构造函数时,比如Java,我得到了另一个抱怨:​​

static mut stamps: Vec<StampedData>;
error: expected one of `!`, `+`, `::`, or `=`, found `;`
static mut stamps: Vec<StampedData>;
                                   ^

我从这些错误中了解到我无法全局声明,然后稍后进行初始化,也无法初始化静态的可变向量。最好的做法是在main()中创建变量并将指针传递给需要它的函数吗?

0 个答案:

没有答案