Skip to content

Commit 0ef6b4a

Browse files
committed
Const Eval: Many small improvements.
1. Capitalize lists. 2. Define const function with italics. 3. Call extern and const qualifiers. We may want to look at calling pub a qualifier as well. 4. Fix enum discriminants to say they take a constant expression. 5. Explain that const functions have restrictions more tersely. 6. Fix link to constant expressions on array literal page.
1 parent 0d06bdf commit 0ef6b4a

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

src/const_eval.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ to be ran.
3838
* [Grouped] expressions.
3939
* [Cast] expressions, except pointer to address and
4040
function pointer to address casts.
41-
* calls of const functions and const methods
41+
* Calls of const functions and const methods
4242

4343
## Const context
4444

4545
A _const context_ is one of the following:
4646

47-
* [array type length expressions]
48-
* repeat expression length expessions
49-
* the initializer of
47+
* [Array type length expressions]
48+
* Repeat expression length expessions
49+
* The initializer of
5050
* [constants]
5151
* [statics]
5252
* [enum discriminants]

src/expressions/array-expr.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ order they are written.
1515

1616
Alternatively there can be exactly two expressions inside the brackets,
1717
separated by a semi-colon. The expression after the `;` must be a have type
18-
`usize` and be a [constant expression](expressions.html#constant-expressions),
18+
`usize` and be a [constant expression],
1919
such as a [literal](tokens.html#literals) or a [constant
2020
item](items/constant-items.html). `[a; b]` creates an array containing `b`
2121
copies of the value of `a`. If the expression after the semi-colon has a value

src/items/enumerations.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ then the discriminant can be directly chosen and accessed.
6868

6969
These enumerations can be cast to integer types with the `as` operator by a
7070
[numeric cast]. The enumeration can optionally specify which integer each
71-
discriminant gets by following the variant name with `=` and then an integer
72-
literal. If the first variant in the declaration is unspecified, then it is set
73-
to zero. For every unspecified discriminant, it is set to one higher than the
74-
previous variant in the declaration.
71+
discriminant gets by following the variant name with `=` followed by a [constant
72+
expression]. If the first variant in the declaration is unspecified, then it is
73+
set to zero. For every other unspecified discriminant, it is set to one higher
74+
than the previous variant in the declaration.
7575

7676
```rust
7777
enum Foo {
@@ -141,6 +141,7 @@ enum ZeroVariants {}
141141
[`mem::discriminant`]: ../std/mem/fn.discriminant.html
142142
[numeric cast]: expressions/operator-expr.html#semantics
143143
[`repr` attribute]: attributes.html#ffi-attributes
144+
[constant expression]: const_eval.html#constant-expressions
144145
[default representation]: type-layout.html#the-default-representation
145146
[primitive representation]: type-layout.html#primitive-representations
146147
[`C` representation]: type-layout.html#the-c-representation

src/items/functions.md

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ opposite functionality to [external blocks]. Whereas external
114114
blocks allow Rust code to call foreign code, extern functions with bodies
115115
defined in Rust code _can be called by foreign code_. They are defined in the
116116
same way as any other Rust function, except that they have the `extern`
117-
modifier.
117+
qualifier.
118118

119119
```rust
120120
// Declares an extern fn, the ABI defaults to "C"
@@ -162,33 +162,32 @@ attributes].
162162

163163
## Const functions
164164

165-
Functions can be `const`, meaning they can be called from within
166-
[const contexts]. When called from a const context, the function is interpreted
167-
by the compiler at compile time. The interpretation happens in the environment
168-
of the compilation target and not the host. So `usize` is `32` bits if you are
169-
compiling against a `32` bit system, irrelevant of whether you are building on
170-
a `64` bit or a `32` bit system.
165+
Functions qualified with the `const` keyword are const functions. _Const
166+
funcions_ can be called from within [const contexts]. When called from a const
167+
context, the function is interpreted by the compiler at compile time. The
168+
interpretation happens in the environment of the compilation target and not the
169+
host. So `usize` is `32` bits if you are compiling against a `32` bit system,
170+
irrelevant of whether you are building on a `64` bit or a `32` bit system.
171171

172-
If a const function is called outside a "const context", it is indistinguishable
172+
If a const function is called outside a [const context], it is indistinguishable
173173
from any other function. You can freely do anything with a const function that
174174
you can do with a regular function.
175175

176-
const functions have various restrictions to makes sure that you cannot define a
177-
const function that can't be evaluated at compile-time. It is, for example, not
178-
possible to write a random number generator as a const function. Calling a
179-
const function at compile-time will always yield the same result as calling it at
180-
runtime, even when called multiple times. There's one exception to this rule:
181-
if you are doing complex floating point operations in extreme situations,
182-
then you might get (very slightly) different results.
183-
It is adviseable to not make array lengths and enum discriminants depend
184-
on floating point computations.
176+
Const functions have various restrictions to makes sure that they can't be
177+
evaluated at compile-time. It is, for example, not possible to write a random
178+
number generator as a const function. Calling a const function at compile-time
179+
will always yield the same result as calling it at runtime, even when called
180+
multiple times. There's one exception to this rule: if you are doing complex
181+
floating point operations in extreme situations, then you might get (very
182+
slightly) different results. It is adviseable to not make array lengths and enum
183+
discriminants depend on floating point computations.
185184

186185
Exhaustive list of permitted structures in const functions:
187186

188187
> **Note**: this list is more restrictive than what you can write in
189188
> regular constants
190189
191-
* type parameters where the parameters only have any [trait bounds]
190+
* Type parameters where the parameters only have any [trait bounds]
192191
of the following kind:
193192
* lifetimes
194193
* `Sized` or [`?Sized`]
@@ -199,16 +198,16 @@ Exhaustive list of permitted structures in const functions:
199198
This rule also applies to type parameters of impl blocks that
200199
contain const methods
201200

202-
* arithmetic and comparison operators on integers
203-
* all boolean operators except for `&&` and `||` which are banned since
201+
* Arithmetic and comparison operators on integers
202+
* All boolean operators except for `&&` and `||` which are banned since
204203
they are short-circuiting.
205-
* any kind of aggregate constructor (array, `struct`, `enum`, tuple, ...)
206-
* calls to other *safe* const functions (whether by function call or method call)
207-
* index expressions on arrays and slices
208-
* field accesses on structs and tuples
209-
* reading from constants (but not statics, not even taking a reference to a static)
204+
* Any kind of aggregate constructor (array, `struct`, `enum`, tuple, ...)
205+
* Calls to other *safe* const functions (whether by function call or method call)
206+
* Index expressions on arrays and slices
207+
* Field accesses on structs and tuples
208+
* Reading from constants (but not statics, not even taking a reference to a static)
210209
* `&` and `*` (only dereferencing of references, not raw pointers)
211-
* casts except for raw pointer to integer casts
210+
* Casts except for raw pointer to integer casts
212211
* `const unsafe fn` is allowed, but the body must consist of safe operations
213212
only and you won't be able to call the `const unsafe fn` from within another
214213
const function even if you use `unsafe`

0 commit comments

Comments
 (0)