Skip to content

Commit 77387cb

Browse files
committed
Note difference in CTFE timing between associated and free constants
I found this difference in timing surprising and other may as well.
1 parent 06f9e61 commit 77387cb

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/items/associated-items.md

+23
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ type that the definition has to implement.
293293
An *associated constant definition* defines a constant associated with a
294294
type. It is written the same as a [constant item].
295295

296+
Unlike [free] constants, associated constant definitions undergo
297+
[constant evaluation] only when referenced.
298+
296299
### Associated Constants Examples
297300

298301
A basic example:
@@ -335,6 +338,24 @@ fn main() {
335338
}
336339
```
337340

341+
[Constant evaluation] timing:
342+
343+
```rust
344+
struct Struct;
345+
346+
impl Struct {
347+
const ID: i32 = 1;
348+
// Definition not immediately evaluated
349+
const PANIC: () = panic!("compile-time panic");
350+
}
351+
352+
fn main() {
353+
assert_eq!(1, Struct::ID);
354+
// Referencing Struct::PANIC causes compilation error
355+
// let _ = Struct::PANIC;
356+
}
357+
```
358+
338359
[_ConstantItem_]: constant-items.md
339360
[_Function_]: functions.md
340361
[_MacroInvocationSemi_]: ../macros.md#macro-invocation
@@ -362,3 +383,5 @@ fn main() {
362383
[regular function parameters]: functions.md#attributes-on-function-parameters
363384
[generic parameters]: generics.md
364385
[where clauses]: generics.md#where-clauses
386+
[free]: ../glossary.md#free-item
387+
[constant evaluation]: ../const_eval.md

0 commit comments

Comments
 (0)