File tree 1 file changed +23
-0
lines changed
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.
293
293
An * associated constant definition* defines a constant associated with a
294
294
type. It is written the same as a [ constant item] .
295
295
296
+ Unlike [ free] constants, associated constant definitions undergo
297
+ [ constant evaluation] only when referenced.
298
+
296
299
### Associated Constants Examples
297
300
298
301
A basic example:
@@ -335,6 +338,24 @@ fn main() {
335
338
}
336
339
```
337
340
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
+
338
359
[ _ConstantItem_ ] : constant-items.md
339
360
[ _Function_ ] : functions.md
340
361
[ _MacroInvocationSemi_ ] : ../macros.md#macro-invocation
@@ -362,3 +383,5 @@ fn main() {
362
383
[ regular function parameters ] : functions.md#attributes-on-function-parameters
363
384
[ generic parameters ] : generics.md
364
385
[ 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