diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2020-07-01 07:42:46 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-01 07:42:46 -0700 |
| commit | 8ac17d14aabddc660dafdc8b385bf7e6cd6e2bad (patch) | |
| tree | 007adb6528d219ee28ebf3f22abe885595f3b127 /src/libstd | |
| parent | 9c6548614be6e4441c4d129897678c1e01cd7717 (diff) | |
| parent | 4224313e2bc3fc08e5eee0519d7b5813c3cad580 (diff) | |
| download | rust-8ac17d14aabddc660dafdc8b385bf7e6cd6e2bad.tar.gz rust-8ac17d14aabddc660dafdc8b385bf7e6cd6e2bad.zip | |
Rollup merge of #73716 - poliorcetics:static-keyword, r=LukasKalbertodt
Document the static keyword Partial fix of #34601. This documents the `static` keyword. It's basically a simplified version of the reference with more examples. @rustbot modify labels: T-doc,C-enhancement
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/keyword_docs.rs | 79 |
1 files changed, 76 insertions, 3 deletions
diff --git a/src/libstd/keyword_docs.rs b/src/libstd/keyword_docs.rs index d972cf6db18..d0716c5205d 100644 --- a/src/libstd/keyword_docs.rs +++ b/src/libstd/keyword_docs.rs @@ -1281,11 +1281,84 @@ mod self_upper_keyword {} #[doc(keyword = "static")] // -/// A place that is valid for the duration of a program. +/// A static item is a value which is valid for the entire duration of your +/// program (a `'static` lifetime). /// -/// The documentation for this keyword is [not yet complete]. Pull requests welcome! +/// On the surface, `static` items seem very similar to [`const`]s: both contain +/// a value, both require type annotations and both can only be initialized with +/// constant functions and values. However, `static`s are notably different in +/// that they represent a location in memory. That means that you can have +/// references to `static` items and potentially even modify them, making them +/// essentially global variables. /// -/// [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. +/// +/// Static items cannot be moved: +/// +/// ```rust,compile_fail,E0507 +/// static VEC: Vec<u32> = vec![]; +/// +/// fn move_vec(v: Vec<u32>) -> Vec<u32> { +/// v +/// } +/// +/// // This line causes an error +/// move_vec(VEC); +/// ``` +/// +/// # Simple `static`s +/// +/// Accessing non-[`mut`] `static` items is considered safe, but some +/// restrictions apply. Most notably, the type of a `static` value needs to +/// implement the [`Sync`] trait, ruling out interior mutability containers +/// like [`RefCell`]. 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); +/// // A static item can be used just like a variable in many cases +/// println!("{:?}", FOO); +/// ``` +/// +/// # Mutable `static`s +/// +/// If a `static` item is declared with the [`mut`] keyword, then it is allowed +/// to be modified by the program. However, accessing mutable `static`s can +/// cause undefined behavior in a number of ways, for example due to data races +/// in a multithreaded context. As such, all accesses to mutable `static`s +/// require an [`unsafe`] block. +/// +/// Despite their unsafety, mutable `static`s are necessary in many contexts: +/// they can be used to represent global state shared by the whole program or in +/// [`extern`] blocks to bind to variables from C libraries. +/// +/// 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 +/// [`Sync`]: marker/trait.Sync.html +/// [`RefCell`]: cell/struct.RefCell.html +/// [Reference]: ../reference/items/static-items.html mod static_keyword {} #[doc(keyword = "struct")] |
