0

I'm attempting to define a static Vec with a struct of my own design for global use in my program (and yes, it does need to be mutable), but I get one of two errors. When given a constructor, the compiler complains and doesn't compile:

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();
                                      ^~~~~~~~~~

and when I omit the constructor as I would in, say, Java, I get another complaint:

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

I understand from these errors that I can't declare globally, then initialize later, nor can I initialize a static, mutable vector. Would the best course of action be to make the variable in main() and pass a pointer to the functions that need it?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Valkyrie
  • 841
  • 1
  • 9
  • 22
  • *Would the best course of action be to make the variable in `main()` and pass a pointer to the functions that need it?* — 100% yes. – Shepmaster Jun 25 '16 at 21:32
  • *When given a destructor...* - did you mean **constructor**? A *destructor* is code that is run when the object is deallocated. – Shepmaster Jun 25 '16 at 21:33
  • @Shepmaster thanks for the affirmation of my pointer idea, and yes, you're correct, I guess I was thinking destructor due to the error – Valkyrie Jun 25 '16 at 21:36

0 Answers0