25.8 Mutable Global Variables

In Rust, global mutable variables are declared with static mut. They are inherently unsafe because concurrent or uncontrolled writes can introduce data races.

#![allow(unused)]
fn main() {
static mut COUNTER: i32 = 0;

fn increment() {
    unsafe {
        COUNTER += 1;
    }
}
}

Minimize the use of mutable globals. When they are truly necessary, consider using synchronization primitives to ensure safe, race-free access.