1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-14 22:15:54 +02:00

Static variables don't need unsafe (#995)

This commit is contained in:
Dominik Maier 2023-07-18 07:23:03 +02:00 committed by GitHub
parent 21ea796e3b
commit cb689be312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -41,9 +41,8 @@ fn main() {
As noted in the [Rust RFC Book][1], these are not inlined upon use and have an actual associated memory location. This is useful for unsafe and embedded code, and the variable lives through the entirety of the program execution.
When a globally-scoped value does not have a reason to need object identity, `const` is generally preferred.
Because `static` variables are accessible from any thread, they need to be guarded, for example by a [`Mutex`](https://doc.rust-lang.org/std/sync/struct.Mutex.html), or accessed using `unsafe` code.
We will look at [mutating static data](../unsafe/mutable-static-variables.md) in the chapter on Unsafe Rust.
Because `static` variables are accessible from any thread, mutable static variables require manual, unsafe, synchronization of accesses.
<details>