Skip to content

Commit

Permalink
Merge pull request #1120 from XrXr/const-eval-panics
Browse files Browse the repository at this point in the history
Note difference in CTFE timing between associated and free constants
  • Loading branch information
ehuss committed Jan 24, 2022
2 parents cd0f699 + be49428 commit e67f679
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/items/associated-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,25 @@ 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.

```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;
}
```

### Associated Constants Examples

A basic example:
Expand Down Expand Up @@ -362,3 +381,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 e67f679

Please sign in to comment.