|
10 | 10 |
|
11 | 11 | #[doc(keyword = "as")]
|
12 | 12 | //
|
13 |
| -/// The type coercion keyword |
| 13 | +/// The type coercion keyword. |
14 | 14 | ///
|
15 | 15 | /// `as` is most commonly used to turn primitive types into other primitive types, but it has other
|
16 | 16 | /// uses that include turning pointers into addresses, addresses into pointers, and pointers into
|
|
35 | 35 | /// [Reference]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions
|
36 | 36 | mod as_keyword { }
|
37 | 37 |
|
| 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 | + |
38 | 93 | #[doc(keyword = "fn")]
|
39 | 94 | //
|
40 | 95 | /// The `fn` keyword.
|
|
0 commit comments