File tree 1 file changed +21
-0
lines changed
1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -293,6 +293,25 @@ 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
+
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
+
296
315
### Associated Constants Examples
297
316
298
317
A basic example:
@@ -362,3 +381,5 @@ fn main() {
362
381
[ regular function parameters ] : functions.md#attributes-on-function-parameters
363
382
[ generic parameters ] : generics.md
364
383
[ where clauses ] : generics.md#where-clauses
384
+ [ free ] : ../glossary.md#free-item
385
+ [ constant evaluation ] : ../const_eval.md
You can’t perform that action at this time.
0 commit comments