Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 253
TurtleBest Avatar asked Apr 15 '15 15:04

TurtleBest


People also ask

How do you make a static string in Rust?

Rust Create Static Variable The first is to create a constant value with a static declaration. An example code is as shown below: static VALUE: i32 = 10; The second method to create a static value is to create a string literal with type &'static str.

How do you change a string to a static value in Java?

you cannot change the value of as static String variable within a local method because of its immutable property.

Can we declare string as static in Java?

If we declare any variable as static means it is class level variable. If we declare any variable as static final its class level variable and its value can not be changed. final static variables are constants in java. We can access these final static variables directly without using object or class name.

Is static variable mutable?

Static variables can be changed ( mutable) but they are stored as only one copy. Any change to them made will be reflected to the all methods using it.


2 Answers

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 :
like image 143
Shepmaster Avatar answered Sep 18 '22 05:09

Shepmaster


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);
    }
}
like image 42
Pascalius Avatar answered Sep 19 '22 05:09

Pascalius