@@ -1030,9 +1030,85 @@ mod self_upper_keyword {}
1030
1030
//
1031
1031
/// A place that is valid for the duration of a program.
1032
1032
///
1033
- /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1033
+ /// A `static` item is similar to a [`const`] item in that it lives for the
1034
+ /// entire duration of the program and need to have its type explicited, with a
1035
+ /// `static` lifetime, outliving any other lifetime. Added to that, `static`
1036
+ /// items represent a precise memory location.
1034
1037
///
1035
- /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
1038
+ /// Static items do not call [`drop`] at the end of the program.
1039
+ ///
1040
+ /// There are two types of `static` items: those declared in association with
1041
+ /// the [`mut`] keyword and those without.
1042
+ ///
1043
+ /// # Simple `static`s
1044
+ ///
1045
+ /// Non-[`mut`] `static` items that contain a type that is not interior mutable
1046
+ /// may be placed in read-only memory. All access to a `static` item are
1047
+ /// considered safe but some restrictions apply. See the [Reference] for more
1048
+ /// information.
1049
+ ///
1050
+ /// ```rust
1051
+ /// static FOO: [i32; 5] = [1, 2, 3, 4, 5];
1052
+ ///
1053
+ /// let r1 = &FOO as *const _;
1054
+ /// let r2 = &FOO as *const _;
1055
+ /// // With a strictly read-only static, references will have the same adress
1056
+ /// assert_eq!(r1, r2);
1057
+ /// ```
1058
+ ///
1059
+ /// # Mutable `static`s
1060
+ ///
1061
+ /// If a `static` item is declared with the [`mut`] keyword, then it is allowed
1062
+ /// to be modified by the program. To make concurrency bugs hard to run into,
1063
+ /// all access to a `static mut` require an [`unsafe`] block. Care should be
1064
+ /// taken to ensure access (both read and write) are thread-safe.
1065
+ ///
1066
+ /// Despite their unsafety, mutable `static`s are very useful: they can be used
1067
+ /// to represent global state shared by the whole program or be used in
1068
+ /// [`extern`] blocks to bind to variables from C libraries.
1069
+ ///
1070
+ /// As global state:
1071
+ ///
1072
+ /// ```rust
1073
+ /// # #![allow(unused_variables)]
1074
+ /// # fn main() {}
1075
+ /// # fn atomic_add(_: &mut u32, _: u32) -> u32 { 2 }
1076
+ /// static mut LEVELS: u32 = 0;
1077
+ ///
1078
+ /// // This violates the idea of no shared state, and this doesn't internally
1079
+ /// // protect against races, so this function is `unsafe`
1080
+ /// unsafe fn bump_levels_unsafe1() -> u32 {
1081
+ /// let ret = LEVELS;
1082
+ /// LEVELS += 1;
1083
+ /// return ret;
1084
+ /// }
1085
+ ///
1086
+ /// // Assuming that we have an atomic_add function which returns the old value,
1087
+ /// // this function is "safe" but the meaning of the return value may not be
1088
+ /// // what callers expect, so it's still marked as `unsafe`
1089
+ /// unsafe fn bump_levels_unsafe2() -> u32 {
1090
+ /// return atomic_add(&mut LEVELS, 1);
1091
+ /// }
1092
+ /// ```
1093
+ ///
1094
+ /// In an [`extern`] block:
1095
+ ///
1096
+ /// ```rust,no_run
1097
+ /// # #![allow(dead_code)]
1098
+ /// extern "C" {
1099
+ /// static mut ERROR_MESSAGE: *mut std::os::raw::c_char;
1100
+ /// }
1101
+ /// ```
1102
+ ///
1103
+ /// Mutable `static`s, just like simple `static`s, have some restrictions that
1104
+ /// apply to them. See the [Reference] for more information.
1105
+ ///
1106
+ /// [`const`]: keyword.const.html
1107
+ /// [`extern`]: keyword.extern.html
1108
+ /// [`mut`]: keyword.mut.html
1109
+ /// [`unsafe`]: keyword.unsafe.html
1110
+ /// [`drop`]: mem/fn.drop.html
1111
+ /// [Reference]: ../reference/items/static-items.html#static-items
1036
1112
mod static_keyword { }
1037
1113
1038
1114
#[ doc( keyword = "struct" ) ]
0 commit comments