diff --git a/src/items/associated-items.md b/src/items/associated-items.md index afbcb5cd5..68e5de163 100644 --- a/src/items/associated-items.md +++ b/src/items/associated-items.md @@ -293,6 +293,25 @@ type that the definition has to implement. An *associated constant definition* defines a constant associated with a type. It is written the same as a [constant item]. +Unlike [free] constants, associated constant definitions undergo +[constant evaluation] only when referenced. + +```rust +struct Struct; + +impl Struct { + const ID: i32 = 1; + // Definition not immediately evaluated + const PANIC: () = panic!("compile-time panic"); +} + +fn main() { + assert_eq!(1, Struct::ID); + // Referencing Struct::PANIC causes compilation error + // let _ = Struct::PANIC; +} +``` + ### Associated Constants Examples A basic example: @@ -362,3 +381,5 @@ fn main() { [regular function parameters]: functions.md#attributes-on-function-parameters [generic parameters]: generics.md [where clauses]: generics.md#where-clauses +[free]: ../glossary.md#free-item +[constant evaluation]: ../const_eval.md