TurtleBest
TurtleBest

Reputation: 71

How do I assign a String to a mutable static variable?

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

Upvotes: 7

Views: 6108

Answers (2)

Shepmaster
Shepmaster

Reputation: 430711

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 :

Upvotes: 9

Pascalius
Pascalius

Reputation: 14659

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);
    }
}

Upvotes: 3

Related Questions