Static Variables are global variables.
Static variables can only store references with the 'static
lifetime.
Static variables can be mutable
Accessing and modifying mutable static variables need to be in unsafe block.
A static variable have a fixed address in memory.
static USER_NAME:&str = "Jim";
fn main() {
println!("{}", USER_NAME)
}
We use static to declare a static variable, whose name should be all uppercase, and need to add type annotation followed by eqaul sign, then the value we want to assign to this variable. And reading an immutable static variable don’t need to be in the unsafe block.
To mutate and access a static variable need to be in the unsafe block:
static mut USER_NAME:&str = "Jim";
fn main() {
unsafe {
println!("{}", USER_NAME);
USER_NAME = "Joe";
println!("{}", USER_NAME);
}
}