6

I want to assign a value to a global variable, but it keeps having a compiler error:

static mut NameArr: [&'static str; 20] = ["\0"; 20];

fn main() {

  unsafe {
    static mut S1 :String = "".to_string();

    S1.push('\0');

    NameArr[0] = S1.as_slice();
  }
}

The error:

a.rs:7:29: 7:43 error: mutable statics are not allowed to have destructors
a.rs:7     static mut S1 :String = "".to_string();
                                   ^~~~~~~~~~~~~~
a.rs:7:29: 7:43 error: static contains unimplemented expression type [E0019]
a.rs:7     static mut S1 :String = "".to_string();
                                   ^~~~~~~~~~~~~~
error: aborting due to 2 previous errors
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
TurtleBest
  • 61
  • 1
  • 3
  • 3
    Why do you want to have a global mutable variable? That strongly pushes against *everything* Rust wants to encourage. The fact that you had to use `unsafe` for basically your entire program is a sign that you are really trying to do something awkward. – Shepmaster Apr 15 '15 at 15:54
  • 4
    What (as distinct from how) are you actually trying to do? It's not at all clear to me from your code example. – Chris Morgan Apr 15 '15 at 16:09
  • Why do you have global `&'static str` with application lifetime? If won't ever get de-allocated and could lead to memory leaks IIUC. – Daniel Fath Apr 15 '15 at 16:31

2 Answers2

8

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:

  1. Rust uses snake_case for variables
  2. SCREAMING_SNAKE_CASE for constants / statics
  3. CamelCase for structs/enums/traits
  4. 4-space indention
  5. Variable definitions should be name: type, with the space after the :
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
2

In Rust you would use lazy_static and a RwLock or Mutex to synchronize write access.

Cargo.toml

[dependencies]
lazy_static = "0.2"

main.rs

#[macro_use]
extern crate lazy_static;

use std::sync::RwLock;

lazy_static! {
    static ref GLOBAL_STRING: RwLock<String> = RwLock::new("string".to_string());
}

fn main() {
    {
        let nice = GLOBAL_STRING.read().unwrap();
        println!("{}", *nice);
    }

    {
        let mut mystr = GLOBAL_STRING.write().unwrap();
        *mystr = "assign new".to_string();
    }

    {
        let nice = GLOBAL_STRING.read().unwrap();
        println!("{}", *nice);
    }
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Pascalius
  • 14,024
  • 4
  • 40
  • 38