Skip to content

Commit a167485

Browse files
committed
Auto merge of rust-lang#75960 - camelid:patch-6, r=jyn514
Improve docs for the `const` keyword @rustbot modify labels: T-doc
2 parents 95815c9 + ce90478 commit a167485

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

library/std/src/keyword_docs.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,20 @@ mod break_keyword {}
107107
/// Sometimes a certain value is used many times throughout a program, and it can become
108108
/// inconvenient to copy it over and over. What's more, it's not always possible or desirable to
109109
/// make it a variable that gets carried around to each function that needs it. In these cases, the
110-
/// `const` keyword provides a convenient alternative to code duplication.
110+
/// `const` keyword provides a convenient alternative to code duplication:
111111
///
112112
/// ```rust
113113
/// const THING: u32 = 0xABAD1DEA;
114114
///
115115
/// let foo = 123 + THING;
116116
/// ```
117117
///
118-
/// Constants must be explicitly typed, unlike with `let` you can't ignore its type and let the
119-
/// compiler figure it out. Any constant value can be defined in a const, which in practice happens
120-
/// to be most things that would be reasonable to have a constant (barring `const fn`s). For
121-
/// example, you can't have a File as a `const`.
118+
/// Constants must be explicitly typed; unlike with `let`, you can't ignore their type and let the
119+
/// compiler figure it out. Any constant value can be defined in a `const`, which in practice happens
120+
/// to be most things that would be reasonable to have in a constant (barring `const fn`s). For
121+
/// example, you can't have a [`File`] as a `const`.
122+
///
123+
/// [`File`]: crate::fs::File
122124
///
123125
/// The only lifetime allowed in a constant is `'static`, which is the lifetime that encompasses
124126
/// all others in a Rust program. For example, if you wanted to define a constant string, it would
@@ -128,27 +130,27 @@ mod break_keyword {}
128130
/// const WORDS: &'static str = "hello rust!";
129131
/// ```
130132
///
131-
/// Thanks to static lifetime elision, you usually don't have to explicitly use 'static:
133+
/// Thanks to static lifetime elision, you usually don't have to explicitly use `'static`:
132134
///
133135
/// ```rust
134136
/// const WORDS: &str = "hello convenience!";
135137
/// ```
136138
///
137139
/// `const` items looks remarkably similar to `static` items, which introduces some confusion as
138140
/// to which one should be used at which times. To put it simply, constants are inlined wherever
139-
/// they're used, making using them identical to simply replacing the name of the const with its
140-
/// value. Static variables on the other hand point to a single location in memory, which all
141+
/// they're used, making using them identical to simply replacing the name of the `const` with its
142+
/// value. Static variables, on the other hand, point to a single location in memory, which all
141143
/// accesses share. This means that, unlike with constants, they can't have destructors, and act as
142144
/// a single value across the entire codebase.
143145
///
144-
/// Constants, as with statics, should always be in SCREAMING_SNAKE_CASE.
146+
/// Constants, like statics, should always be in `SCREAMING_SNAKE_CASE`.
145147
///
146148
/// The `const` keyword is also used in raw pointers in combination with `mut`, as seen in `*const
147-
/// T` and `*mut T`. More about that can be read at the [pointer] primitive part of the Rust docs.
149+
/// T` and `*mut T`. More about `const` as used in raw pointers can be read at the Rust docs for the [pointer primitive].
148150
///
149-
/// For more detail on `const`, see the [Rust Book] or the [Reference]
151+
/// For more detail on `const`, see the [Rust Book] or the [Reference].
150152
///
151-
/// [pointer]: primitive.pointer.html
153+
/// [pointer primitive]: primitive.pointer.html
152154
/// [Rust Book]:
153155
/// ../book/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants
154156
/// [Reference]: ../reference/items/constant-items.html

0 commit comments

Comments
 (0)