You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/expressions/block-expr.md
+7-3
Original file line number
Diff line number
Diff line change
@@ -129,7 +129,7 @@ Const blocks allows you to define a constant value without having to define new
129
129
It also supports type inference so there is no need to specify the type, unlike [constant items].
130
130
131
131
Const blocks have the ability to reference generic parameters in scope, unlike [free][free item] constant items.
132
-
They are desugared to associated constant items with generic parameters in scope.
132
+
They are desugared to constant items with generic parameters in scope (similar to associated constants, but without a trait or type they are associated with).
133
133
For example, this code:
134
134
135
135
```rust
@@ -152,8 +152,12 @@ fn foo<T>() -> usize {
152
152
}
153
153
```
154
154
155
-
This also means that const blocks are treated similarly to associated constants.
156
-
For example, they are not guaranteed to be evaluated when the enclosing function is unused.
155
+
If the function containing the const block is executed at runtime, then the const block is guaranteed to be evaluated.
156
+
This is true even if the const block occurs in dead code.
157
+
In particular, unsafe code inside a function can rely on the fact that all const blocks syntactically occurring in the function have successfully evaluated (and not panicked).
158
+
Note that this is only guaranteed for all currently existing syntactic constructs;
159
+
in the future, a new kind of block may be introduced such that constants inside that block are *not* unconditionally guaranteed to be evaluated.
160
+
If the function is not executed at runtime, const blocks inside of it may or may not be evaluated.
0 commit comments