From 7bffb7f2c4d2a0301fb7c98e5b0597bde8727f08 Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Mon, 9 Oct 2023 15:15:26 +0200 Subject: [PATCH] Update mutable-static-variables.md (#1331) From a discussion in #1297. --- src/unsafe/mutable-static-variables.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/unsafe/mutable-static-variables.md b/src/unsafe/mutable-static-variables.md index 75cf49e3..6383a2f3 100644 --- a/src/unsafe/mutable-static-variables.md +++ b/src/unsafe/mutable-static-variables.md @@ -29,7 +29,11 @@ fn main() {
-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. +- 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.