Skip to content

Commit

Permalink
attempt to clarify 'static' unique address guarantees
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Oct 25, 2024
1 parent 46d65f8 commit 35ae17a
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/items/static-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
> [^extern-safety]: The `safe` and `unsafe` function qualifiers are only
> allowed semantically within `extern` blocks.
A *static item* is similar to a [constant], except that it represents a precise
memory location in the program. All references to the static refer to the same
memory location. Static items have the `static` lifetime, which outlives all
other lifetimes in a Rust program. Static items do not call [`drop`] at the
end of the program.
A *static item* is similar to a [constant], except that it represents an allocated object in the
program that is initialized with the initializer expression. All references and raw pointers to the
static refer to the same allocated object. Static items have the `static` lifetime, which outlives
all other lifetimes in a Rust program. Static items do not call [`drop`] at the end of the program.

This allocated object is disjoint from all other `static` objects as well as heap-allocated and
stack-allocated variables. However, the storage of immutable `static` items can overlap with objects
that do not themselves have a unique address, such as promoteds and `const` items.

The static declaration defines a static value in the [value namespace] of the module or block where it is located.

The static initializer is a [constant expression] evaluated at compile time.
Static initializers may refer to other statics.
Static initializers may refer to and even read from other statics.
When reading from mutable statics, they read the initial value of that static.

Non-`mut` static items that contain a type that is not [interior mutable] may
be placed in read-only memory.
Expand Down

0 comments on commit 35ae17a

Please sign in to comment.