Skip to content

Commit

Permalink
Note difference in CTFE timing between associated and free constants
Browse files Browse the repository at this point in the history
I found this difference in timing surprising and other may as well.
  • Loading branch information
XrXr committed Jan 20, 2022
1 parent 06f9e61 commit 77387cb
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/items/associated-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ type that the definition has to implement.
An *associated constant definition* defines a constant associated with a
type. It is written the same as a [constant item].

Unlike [free] constants, associated constant definitions undergo
[constant evaluation] only when referenced.

### Associated Constants Examples

A basic example:
Expand Down Expand Up @@ -335,6 +338,24 @@ fn main() {
}
```

[Constant evaluation] timing:

```rust
struct Struct;

impl Struct {
const ID: i32 = 1;
// Definition not immediately evaluated
const PANIC: () = panic!("compile-time panic");
}

fn main() {
assert_eq!(1, Struct::ID);
// Referencing Struct::PANIC causes compilation error
// let _ = Struct::PANIC;
}
```

[_ConstantItem_]: constant-items.md
[_Function_]: functions.md
[_MacroInvocationSemi_]: ../macros.md#macro-invocation
Expand Down Expand Up @@ -362,3 +383,5 @@ fn main() {
[regular function parameters]: functions.md#attributes-on-function-parameters
[generic parameters]: generics.md
[where clauses]: generics.md#where-clauses
[free]: ../glossary.md#free-item
[constant evaluation]: ../const_eval.md

0 comments on commit 77387cb

Please sign in to comment.