1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-21 23:45:42 +02:00
comprehensive-rust/src/unsafe-rust/mutable-static.md
Andrew Walbran b808887006
Add safety comments and use consistent format for existing ones. (#1981)
We should have safety comments on all `unsafe` blocks, to set a good
example.
2024-04-12 13:19:19 -04:00

52 lines
1.2 KiB
Markdown

---
minutes: 5
---
# Mutable Static Variables
It is safe to read an immutable static variable:
```rust,editable
static HELLO_WORLD: &str = "Hello, world!";
fn main() {
println!("HELLO_WORLD: {HELLO_WORLD}");
}
```
However, since data races can occur, it is unsafe to read and write mutable
static variables:
```rust,editable
static mut COUNTER: u32 = 0;
fn add_to_counter(inc: u32) {
// SAFETY: There are no other threads which could be accessing `COUNTER`.
unsafe {
COUNTER += inc;
}
}
fn main() {
add_to_counter(42);
// SAFETY: There are no other threads which could be accessing `COUNTER`.
unsafe {
println!("COUNTER: {COUNTER}");
}
}
```
<details>
- The program here is safe because it is single-threaded. However, the Rust
compiler is conservative and will assume the worst. Try removing the `unsafe`
and see how the compiler explains that it is undefined behavior to mutate a
static from multiple threads.
- Using a mutable static is generally a bad idea, but there are some cases where
it might make sense in low-level `no_std` code, such as implementing a heap
allocator or working with some C APIs.
</details>