rust-lang / reference

The Rust Reference
https://doc.rust-lang.org/nightly/reference/
Apache License 2.0
1.25k stars 493 forks source link

Update mutable static items example for modern Rust. #1544

Closed ehuss closed 4 months ago

ehuss commented 4 months ago

This updates the mutable static item example to fix some issues with how it works and how it is presented.

First, the unsafe operations are wrapped in an unsafe block so that it doesn't trigger unsafe_op_in_unsafe_fn.

Second, it switches the mutation example to use addr_of_mut to avoid the undefined behavior, and to compile correctly in 2024 edition.

Third, it rewrites the second example to be safe with a different description. My understanding is that the original example was written at a time when it was considered that any potential race condition was considered unsafe. However, that is no longer a widely held view. The example has been rewritten to illustrate the two different ways you can wrap mutable static access.

This is an alternative to https://github.com/rust-lang/reference/pull/1349.