File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -293,6 +293,9 @@ type that the definition has to implement.
293293An * associated constant definition* defines a constant associated with a
294294type. 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
298301A 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
You can’t perform that action at this time.
0 commit comments