Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0c88dd6
Update Box::from_raw example to generalize better
Keno Jun 24, 2020
2bbc2b3
Document the static keyword
poliorcetics Jun 25, 2020
00ef461
Merge pull request #2 from rust-lang/master
TyPR124 Jun 26, 2020
b71a3e1
Map ERROR_INVALID_PARAMETER to InvalidInput
TyPR124 Jun 26, 2020
0d0865f
Make `rustc_peek` a safe intrinsic
oli-obk Jun 27, 2020
765bd47
Recover extra trailing angle brackets in struct definition
Aaron1011 Jun 27, 2020
3fc5593
Document the type keyword
poliorcetics Jun 27, 2020
517d361
Use an 'approximate' universal upper bound when reporting region errors
Aaron1011 Jun 27, 2020
7055c23
ast_pretty: Pass some token streams and trees by reference
petrochenkov Jun 24, 2020
14d0370
Remove defunct `-Z print-region-graph`
tmiasko Jun 28, 2020
7231e57
Fix wording for anonymous parameter name help
nop Jun 28, 2020
e611a3f
Apply suggestions from code review
poliorcetics Jun 28, 2020
e8f5785
Split and expand nonstandard-style lints unicode unit test.
crlf0710 Jun 28, 2020
dfd454b
Apply suggestions, reformulating some paragraphs and improving some e…
poliorcetics Jun 28, 2020
824b2bb
Match on `Symbol` instead of `&str` for type-checking intrinsics.
oli-obk Jun 28, 2020
4224313
Fix small nits
poliorcetics Jun 28, 2020
4966272
Add newline to rustc MultiSpan docs
pierwill Jun 28, 2020
dd34698
Fix Zulip topic format
LeSeulArtichaut Jun 29, 2020
d8e0987
Rollup merge of #73678 - Keno:patch-1, r=LukasKalbertodt
Manishearth Jun 30, 2020
ffe4a21
Rollup merge of #73716 - poliorcetics:static-keyword, r=LukasKalbertodt
Manishearth Jun 30, 2020
7a3bc62
Rollup merge of #73752 - TyPR124:invalid-parameter-error, r=LukasKalb…
Manishearth Jun 30, 2020
fdd6590
Rollup merge of #73803 - Aaron1011:feature/angle-field-recovery, r=ma…
Manishearth Jun 30, 2020
86d4dc0
Rollup merge of #73805 - poliorcetics:type-keyword, r=kennytm
Manishearth Jun 30, 2020
0e585c3
Rollup merge of #73806 - Aaron1011:feature/approx-universal-upper, r=…
Manishearth Jun 30, 2020
0d2c65a
Rollup merge of #73812 - petrochenkov:prettyref, r=varkor
Manishearth Jun 30, 2020
1402233
Rollup merge of #73828 - nop:fix/parameter-name-help, r=estebank
Manishearth Jun 30, 2020
691ba6f
Rollup merge of #73834 - oli-obk:safe_intrinsics, r=ecstatic-morse
Manishearth Jun 30, 2020
366015b
Rollup merge of #73839 - crlf0710:snapshot_the_reality, r=Manishearth
Manishearth Jun 30, 2020
f7d23f4
Rollup merge of #73841 - tmiasko:print-region-graph, r=Mark-Simulacrum
Manishearth Jun 30, 2020
051117b
Rollup merge of #73853 - pierwill:pierwill-multispan-doc, r=jonas-sch…
Manishearth Jun 30, 2020
7f598c0
Rollup merge of #73865 - LeSeulArtichaut:patch-1, r=Dylan-DPC
Manishearth Jun 30, 2020
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
Prev Previous commit
Next Next commit
Document the static keyword
  • Loading branch information
poliorcetics committed Jun 25, 2020
commit 2bbc2b3de42f3b14ccc8b62c2acffc8840177777
80 changes: 78 additions & 2 deletions src/libstd/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,9 +1030,85 @@ mod self_upper_keyword {}
//
/// A place that is valid for the duration of a program.
///
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
/// A `static` item is similar to a [`const`] item in that it lives for the
/// entire duration of the program and need to have its type explicited, with a
/// `static` lifetime, outliving any other lifetime. Added to that, `static`
/// items represent a precise memory location.
///
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
/// Static items do not call [`drop`] at the end of the program.
///
/// There are two types of `static` items: those declared in association with
/// the [`mut`] keyword and those without.
///
/// # Simple `static`s
///
/// Non-[`mut`] `static` items that contain a type that is not interior mutable
/// may be placed in read-only memory. All access to a `static` item are
/// considered safe but some restrictions apply. See the [Reference] for more
/// information.
///
/// ```rust
/// static FOO: [i32; 5] = [1, 2, 3, 4, 5];
///
/// let r1 = &FOO as *const _;
/// let r2 = &FOO as *const _;
/// // With a strictly read-only static, references will have the same adress
/// assert_eq!(r1, r2);
/// ```
///
/// # Mutable `static`s
///
/// If a `static` item is declared with the [`mut`] keyword, then it is allowed
/// to be modified by the program. To make concurrency bugs hard to run into,
/// all access to a `static mut` require an [`unsafe`] block. Care should be
/// taken to ensure access (both read and write) are thread-safe.
///
/// Despite their unsafety, mutable `static`s are very useful: they can be used
/// to represent global state shared by the whole program or be used in
/// [`extern`] blocks to bind to variables from C libraries.
///
/// As global state:
///
/// ```rust
/// # #![allow(unused_variables)]
/// # fn main() {}
/// # fn atomic_add(_: &mut u32, _: u32) -> u32 { 2 }
/// static mut LEVELS: u32 = 0;
///
/// // This violates the idea of no shared state, and this doesn't internally
/// // protect against races, so this function is `unsafe`
/// unsafe fn bump_levels_unsafe1() -> u32 {
/// let ret = LEVELS;
/// LEVELS += 1;
/// return ret;
/// }
///
/// // Assuming that we have an atomic_add function which returns the old value,
/// // this function is "safe" but the meaning of the return value may not be
/// // what callers expect, so it's still marked as `unsafe`
/// unsafe fn bump_levels_unsafe2() -> u32 {
/// return atomic_add(&mut LEVELS, 1);
/// }
/// ```
///
/// In an [`extern`] block:
///
/// ```rust,no_run
/// # #![allow(dead_code)]
/// extern "C" {
/// static mut ERROR_MESSAGE: *mut std::os::raw::c_char;
/// }
/// ```
///
/// Mutable `static`s, just like simple `static`s, have some restrictions that
/// apply to them. See the [Reference] for more information.
///
/// [`const`]: keyword.const.html
/// [`extern`]: keyword.extern.html
/// [`mut`]: keyword.mut.html
/// [`unsafe`]: keyword.unsafe.html
/// [`drop`]: mem/fn.drop.html
/// [Reference]: ../reference/items/static-items.html#static-items
mod static_keyword {}

#[doc(keyword = "struct")]
Expand Down