@@ -1281,11 +1281,84 @@ mod self_upper_keyword {}
1281
1281
1282
1282
#[ doc( keyword = "static" ) ]
1283
1283
//
1284
- /// A place that is valid for the duration of a program.
1284
+ /// A static item is a value which is valid for the entire duration of your
1285
+ /// program (a `'static` lifetime).
1285
1286
///
1286
- /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1287
+ /// On the surface, `static` items seem very similar to [`const`]s: both contain
1288
+ /// a value, both require type annotations and both can only be initialized with
1289
+ /// constant functions and values. However, `static`s are notably different in
1290
+ /// that they represent a location in memory. That means that you can have
1291
+ /// references to `static` items and potentially even modify them, making them
1292
+ /// essentially global variables.
1287
1293
///
1288
- /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
1294
+ /// Static items do not call [`drop`] at the end of the program.
1295
+ ///
1296
+ /// There are two types of `static` items: those declared in association with
1297
+ /// the [`mut`] keyword and those without.
1298
+ ///
1299
+ /// Static items cannot be moved:
1300
+ ///
1301
+ /// ```rust,compile_fail,E0507
1302
+ /// static VEC: Vec<u32> = vec![];
1303
+ ///
1304
+ /// fn move_vec(v: Vec<u32>) -> Vec<u32> {
1305
+ /// v
1306
+ /// }
1307
+ ///
1308
+ /// // This line causes an error
1309
+ /// move_vec(VEC);
1310
+ /// ```
1311
+ ///
1312
+ /// # Simple `static`s
1313
+ ///
1314
+ /// Accessing non-[`mut`] `static` items is considered safe, but some
1315
+ /// restrictions apply. Most notably, the type of a `static` value needs to
1316
+ /// implement the [`Sync`] trait, ruling out interior mutability containers
1317
+ /// like [`RefCell`]. See the [Reference] for more information.
1318
+ ///
1319
+ /// ```rust
1320
+ /// static FOO: [i32; 5] = [1, 2, 3, 4, 5];
1321
+ ///
1322
+ /// let r1 = &FOO as *const _;
1323
+ /// let r2 = &FOO as *const _;
1324
+ /// // With a strictly read-only static, references will have the same adress
1325
+ /// assert_eq!(r1, r2);
1326
+ /// // A static item can be used just like a variable in many cases
1327
+ /// println!("{:?}", FOO);
1328
+ /// ```
1329
+ ///
1330
+ /// # Mutable `static`s
1331
+ ///
1332
+ /// If a `static` item is declared with the [`mut`] keyword, then it is allowed
1333
+ /// to be modified by the program. However, accessing mutable `static`s can
1334
+ /// cause undefined behavior in a number of ways, for example due to data races
1335
+ /// in a multithreaded context. As such, all accesses to mutable `static`s
1336
+ /// require an [`unsafe`] block.
1337
+ ///
1338
+ /// Despite their unsafety, mutable `static`s are necessary in many contexts:
1339
+ /// they can be used to represent global state shared by the whole program or in
1340
+ /// [`extern`] blocks to bind to variables from C libraries.
1341
+ ///
1342
+ /// In an [`extern`] block:
1343
+ ///
1344
+ /// ```rust,no_run
1345
+ /// # #![allow(dead_code)]
1346
+ /// extern "C" {
1347
+ /// static mut ERROR_MESSAGE: *mut std::os::raw::c_char;
1348
+ /// }
1349
+ /// ```
1350
+ ///
1351
+ /// Mutable `static`s, just like simple `static`s, have some restrictions that
1352
+ /// apply to them. See the [Reference] for more information.
1353
+ ///
1354
+ /// [`const`]: keyword.const.html
1355
+ /// [`extern`]: keyword.extern.html
1356
+ /// [`mut`]: keyword.mut.html
1357
+ /// [`unsafe`]: keyword.unsafe.html
1358
+ /// [`drop`]: mem/fn.drop.html
1359
+ /// [`Sync`]: marker/trait.Sync.html
1360
+ /// [`RefCell`]: cell/struct.RefCell.html
1361
+ /// [Reference]: ../reference/items/static-items.html
1289
1362
mod static_keyword { }
1290
1363
1291
1364
#[ doc( keyword = "struct" ) ]
0 commit comments