Skip to content

Commit e67f679

Browse files
authored
Merge pull request #1120 from XrXr/const-eval-panics
Note difference in CTFE timing between associated and free constants
2 parents cd0f699 + be49428 commit e67f679

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/items/associated-items.md

+21
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,25 @@ 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+
299+
```rust
300+
struct Struct;
301+
302+
impl Struct {
303+
const ID: i32 = 1;
304+
// Definition not immediately evaluated
305+
const PANIC: () = panic!("compile-time panic");
306+
}
307+
308+
fn main() {
309+
assert_eq!(1, Struct::ID);
310+
// Referencing Struct::PANIC causes compilation error
311+
// let _ = Struct::PANIC;
312+
}
313+
```
314+
296315
### Associated Constants Examples
297316

298317
A basic example:
@@ -362,3 +381,5 @@ fn main() {
362381
[regular function parameters]: functions.md#attributes-on-function-parameters
363382
[generic parameters]: generics.md
364383
[where clauses]: generics.md#where-clauses
384+
[free]: ../glossary.md#free-item
385+
[constant evaluation]: ../const_eval.md

0 commit comments

Comments
 (0)