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
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.
you cannot change the value of as static String variable within a local method because of its immutable property.
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.
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.
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:
snake_case
for variablesSCREAMING_SNAKE_CASE
for constants / staticsCamelCase
for structs/enums/traitsname: type
, with the space after the :
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With