Skip to content

Commit c1bd8a9

Browse files
committed
Add keyword docs on const
Turns out writing docs on keywords that are used in multiple different places in entirely different contexts gets a little harder. I put a footnote on `*const` syntax just to make sure you can find it if need be, but it might need more detail.
1 parent 1142bbd commit c1bd8a9

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

src/libstd/keyword_docs.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#[doc(keyword = "as")]
1212
//
13-
/// The type coercion keyword
13+
/// The type coercion keyword.
1414
///
1515
/// `as` is most commonly used to turn primitive types into other primitive types, but it has other
1616
/// uses that include turning pointers into addresses, addresses into pointers, and pointers into
@@ -35,6 +35,61 @@
3535
/// [Reference]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
3636
mod as_keyword { }
3737

38+
#[doc(keyword = "const")]
39+
//
40+
/// The keyword for defining constants.
41+
///
42+
/// Sometimes a certain value is used many times throughout a program, and it can become
43+
/// inconvenient to copy it over and over. What's more, it's not always possible or desirable to
44+
/// make it a variable that gets carried around to each function that needs it. In these cases, the
45+
/// `const` keyword provides a convenient alternative to code duplication.
46+
///
47+
/// ```rust
48+
/// const THING: u32 = 0xABAD1DEA;
49+
///
50+
/// let foo = 123 + THING;
51+
/// ```
52+
///
53+
/// Constants must be explicitly typed, unlike with `let` you can't ignore its type and let the
54+
/// compiler figure it out. Any constant value can be defined in a const, which in practice happens
55+
/// to be most things that would be reasonable to have a constant. For example, you can't have a
56+
/// File as a const.
57+
///
58+
/// The only lifetime allowed in a constant is 'static, which is the lifetime that encompasses all
59+
/// others in a Rust program. For example, if you wanted to define a constant string, it would look
60+
/// like this:
61+
///
62+
/// ```rust
63+
/// const WORDS: &'static str = "hello rust!";
64+
/// ```
65+
///
66+
/// Thanks to static lifetime elision, you usually don't have to explicitly use 'static:
67+
///
68+
/// ```rust
69+
/// const WORDS: &str = "hello convenience!";
70+
/// ```
71+
///
72+
/// `const` items looks remarkably similar to [`static`] items, which introduces some confusion as
73+
/// to which one should be used at which times. To put it simply, constants are inlined wherever
74+
/// they're used, making using them identical to simply replacing the name of the const with its
75+
/// value. Static variables on the other hand point to a single location in memory, which all
76+
/// accesses share. This means that, unlike with constants, they can't have destructors, but it
77+
/// also means that (via unsafe code) they can be mutable, which is useful for the rare situations
78+
/// in which you can't avoid using global state.
79+
///
80+
/// Constants, as with statics, should always be in SCREAMING_SNAKE_CASE.
81+
///
82+
/// The `const` keyword is also used in raw pointers in combination with `mut`, as seen in `*const
83+
/// T` and `*mut T`. More about that can be read at the [pointer] primitive part of the Rust docs.
84+
///
85+
/// For more detail on `const`, see the [Rust Book] or the [Reference]
86+
///
87+
/// [`static`]: keyword.static.html
88+
/// [pointer]: primitive.pointer.html
89+
/// [Rust Book]: https://doc.rust-lang.org/stable/book/2018-edition/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants
90+
/// [Reference]: https://doc.rust-lang.org/reference/items/constant-items.html
91+
mod const_keyword { }
92+
3893
#[doc(keyword = "fn")]
3994
//
4095
/// The `fn` keyword.

0 commit comments

Comments
 (0)