Skip to content

Statics may sometimes inline. #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/items/static-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
> `=` [_Expression_] `;`

A *static item* is similar to a [constant], except that it represents a precise
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This paragraph is just a list of properties, which makes me feel like it does not flow very well. I'm not sure the best way to address that; perhaps splitting the things about lifetimes into a separate paragraph would help, or adding more connective prose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It definitely doesn't flow well. It reads almost like a list but without the bullet points. Should probably be addressed, but not as part of this issue.

memory location in the program. A static is never "inlined" at the usage site,
and all references to it refer to the same memory location. Static items have
the `static` lifetime, which outlives all other lifetimes in a Rust program.
Static items may be placed in read-only memory if the type is not [interior
mutable]. Static items do not call `drop` at the end of the program.
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be 'static lifetime instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'static is the static lifetime. I think both could work there.

other lifetimes in a Rust program. Non-`mut` static items that contain a type
that is not [interior mutable] may be placed in read-only memory. Static items
do not call [`drop`] at the end of the program.

All access to a static is safe, but there are a number of restrictions on
statics:

* The type must have the `Sync` trait bound to allow thread-safe access.
* Statics allow using paths to statics in the
[constant-expression](expressions.html#constant-expressions) used to
* Statics allow using paths to statics in the [constant-expression] used to
initialize them, but statics may not refer to other statics by value, only
through a reference.
* Constants cannot refer to statics.
Expand All @@ -33,7 +32,7 @@ modifications to a mutable static are safe with respect to other threads
running in the same process.

Mutable statics are still very useful, however. They can be used with C
libraries and can also be bound from C libraries (in an `extern` block).
libraries and can also be bound from C libraries in an `extern` block.

```rust
# fn atomic_add(_: &mut u32, _: u32) -> u32 { 2 }
Expand Down Expand Up @@ -66,10 +65,12 @@ item. Constants should, in general, be preferred over statics unless one of the
following are true:

* Large amounts of data are being stored
* The single-address or non-inlining property of statics is required.
* The single-address property of statics is required.
* Interior mutability is required.

[constant]: items/constant-items.html
[`drop`]: destructors.html
[constant expression]: expressions.html#constant-expressions
[interior mutable]: interior-mutability.html
[IDENTIFIER]: identifiers.html
[_Type_]: types.html
Expand Down