Skip to content

Commit f9db008

Browse files
authored
Rollup merge of #77838 - RalfJung:const-fn, r=kennytm
const keyword: brief paragraph on 'const fn' `const fn` were mentioned in the title, but called "deterministic functions" which is not their main property (though at least currently it is a consequence of being const-evaluable). This adds a brief paragraph discussing them, also in the hopes of clarifying that they do *not* have any effect on run-time uses.
2 parents 24907f3 + c8405d2 commit f9db008

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

library/std/src/keyword_docs.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ mod break_keyword {}
102102

103103
#[doc(keyword = "const")]
104104
//
105-
/// Compile-time constants and deterministic functions.
105+
/// Compile-time constants and compile-time evaluable functions.
106+
///
107+
/// ## Compile-time constants
106108
///
107109
/// Sometimes a certain value is used many times throughout a program, and it can become
108110
/// inconvenient to copy it over and over. What's more, it's not always possible or desirable to
@@ -145,15 +147,28 @@ mod break_keyword {}
145147
///
146148
/// Constants, like statics, should always be in `SCREAMING_SNAKE_CASE`.
147149
///
150+
/// For more detail on `const`, see the [Rust Book] or the [Reference].
151+
///
152+
/// ## Compile-time evaluable functions
153+
///
154+
/// The other main use of the `const` keyword is in `const fn`. This marks a function as being
155+
/// callable in the body of a `const` or `static` item and in array initializers (commonly called
156+
/// "const contexts"). `const fn` are restricted in the set of operations they can perform, to
157+
/// ensure that they can be evaluated at compile-time. See the [Reference][const-eval] for more
158+
/// detail.
159+
///
160+
/// Turning a `fn` into a `const fn` has no effect on run-time uses of that function.
161+
///
162+
/// ## Other uses of `const`
163+
///
148164
/// The `const` keyword is also used in raw pointers in combination with `mut`, as seen in `*const
149165
/// T` and `*mut T`. More about `const` as used in raw pointers can be read at the Rust docs for the [pointer primitive].
150166
///
151-
/// For more detail on `const`, see the [Rust Book] or the [Reference].
152-
///
153167
/// [pointer primitive]: primitive.pointer.html
154168
/// [Rust Book]:
155169
/// ../book/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants
156170
/// [Reference]: ../reference/items/constant-items.html
171+
/// [const-eval]: ../reference/const_eval.html
157172
mod const_keyword {}
158173

159174
#[doc(keyword = "continue")]

0 commit comments

Comments
 (0)