File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -91,6 +91,32 @@ m!(const _: () = (););
91
91
// const _: () = ();
92
92
```
93
93
94
+ ## Use and reference to ` static ` items
95
+
96
+ When a constant item or constant block is defined, [ ` static ` items] can be used, borrowed or taken address of.
97
+ By extension, you are allowed to call methods that immutably borrows the ` static ` items as receivers.
98
+
99
+ ``` rust
100
+ static A : u32 = 32 ;
101
+ const ANOTHER_A : u32 = A ;
102
+ const BORROW_A : & 'static u32 = & A ;
103
+ const POINTER_TO_A : * const u32 = & A as _ ;
104
+
105
+ struct MyStruct {
106
+ inner : u32 ,
107
+ }
108
+ impl MyStruct {
109
+ const fn get (& self ) -> u32 {
110
+ self . inner + 1
111
+ }
112
+ }
113
+ static MYSTRUCT : MyStruct = MyStruct {
114
+ inner : 0
115
+ };
116
+ const BORROW_STATIC_INNER : & 'static u32 = & MYSTRUCT . inner;
117
+ const CALL_CONST_STATIC_ASSOCIATED_METHOD : u32 = MYSTRUCT . get ();
118
+ ```
119
+
94
120
## Evaluation
95
121
96
122
[ Free] [ free ] constants are always [ evaluated] [ const_eval ] at compile-time to surface
@@ -111,6 +137,7 @@ fn unused_generic_function<T>() {
111
137
[ constant value ] : ../const_eval.md#constant-expressions
112
138
[ free ] : ../glossary.md#free-item
113
139
[ static lifetime elision ] : ../lifetime-elision.md#static-lifetime-elision
140
+ [ `static` items ] : ./static-items.md
114
141
[ trait definition ] : traits.md
115
142
[ IDENTIFIER ] : ../identifiers.md
116
143
[ underscore imports ] : use-declarations.md#underscore-imports
You can’t perform that action at this time.
0 commit comments