Rust declares a mutable global variable with static mut. The variable remains available throughout the program, but reading or modifying it requires an unsafe block. section 12.6.1 section 12.12
Use the feature with these rules and cautions:
- Declare the variable at global scope, conventionally using an uppercase name such as static mut SHUT_DOWN: bool = false;. section 12.6.1
- Wrap both writes and reads in unsafe blocks, including a seemingly harmless comparison. section 12.6.1
- Treat unsafe as a warning that safety is no longer automatically guaranteed, rather than as an indication of illegal code. section 3.4.1
- Avoid global state when possible because shared mutable access can make code cluttered and can create race-condition risks. section 12.6.1 section 12.12
A global shutdown flag
A program can set SHUT_DOWN to a random Boolean inside an unsafe block, print a dot, and then read the flag inside another unsafe expression. The loop stops when the flag becomes true. This small program makes the access boundary visible while modeling a flag that a signal handler might set. section 12.6.1

