File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 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.
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+
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
298317A 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
You can’t perform that action at this time.
0 commit comments