Skip to content

Commit 9340d52

Browse files
possible document about Freeze
1 parent 76c539f commit 9340d52

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/const_eval.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,30 @@ surrounding generic parameters: such an expression must either be a single bare
6767
const generic parameter, or an arbitrary expression not making use of any
6868
generics.
6969

70+
## Permitted use of `static`s
71+
72+
Within the constant evaluation context, items that ultimately contain no mutable memory or data that is
73+
capable of interior mutability can be used, borrowed or taken address of.
74+
Among those are the `static` items as long as they are not actually `mut` items.
75+
76+
```rust
77+
const fn allowed() -> &'static u32 {
78+
static VALUE: u32 = 0;
79+
&VALUE
80+
}
81+
const A_CONST: &'static u32 = allowed();
82+
```
83+
84+
On the contrary, for example, `static mut` and types embedding an `UnsafeCell` is not allowed.
85+
86+
```rust,edition2021,compile_fail
87+
const fn not_allowed() -> &'static u32 {
88+
static mut VALUE: u32 = 0;
89+
&VALUE
90+
}
91+
const WILL_FAIL: &'static u32 = not_allowed();
92+
```
93+
7094
## Const Functions
7195

7296
A _const fn_ is a function that one is permitted to call from a const context. Declaring a function

0 commit comments

Comments
 (0)