I have no idea what you are trying to do, so I can't tell you how to do it.
Maybe you want How do I create a global, mutable singleton?
That being said, I can help explain your errors:
static mut NameArr: [&'static str; 20] = ["\0"; 20];
This declares a mutable global variable. The variable is an array of a fixed-length of 20 items. Each item is a &'static str
, a string literal that must be guaranteed to live for the life of the entire program. That's what 'static
means.
static mut S1: String = "".to_string();
This attempts to create another mutable global variable, this time it is a String
, an object that allocates and owns a piece of memory in the heap. When a String
goes out of scope, the memory is freed via a destructor. This is the source of your first error - global variables aren't allowed to have destructors. Additionally, you aren't currently allowed to call methods when defining a global value - there's no context for these methods to run right now!
Having global mutable variables is really not a good idea with Rust, or any program, really. There's technical reasons (such as data races) and programmer reasons (rationalizing about this kind of code is hard). You can check out How do I create a global, mutable singleton? for instructions on how to do it if you really need it.
Rust forces you to use an unsafe
block to deal with mutable global variables for these reasons. The compiler can no longer guarantee the safety of your program. For example, what happens to the value you've stored in the array if you were to call S1.clear
?
There are also stylistic concerns:
- Rust uses
snake_case
for variables
SCREAMING_SNAKE_CASE
for constants / statics
CamelCase
for structs/enums/traits
- 4-space indention
- Variable definitions should be
name: type
, with the space after the :