Skip to content

Commit 0b8ced4

Browse files
dingxiangfei2009ehuss
authored andcommitted
possible document about Freeze
1 parent 1462eda commit 0b8ced4

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
@@ -71,6 +71,30 @@ surrounding generic parameters: such an expression must either be a single bare
7171
const generic parameter, or an arbitrary expression not making use of any
7272
generics.
7373

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

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

0 commit comments

Comments
 (0)