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
+31Lines changed: 31 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -117,6 +117,37 @@ loop {
117
117
}
118
118
```
119
119
120
+
## `const` blocks
121
+
122
+
> **<sup>Syntax</sup>**\
123
+
> _ConstBlockExpression_ :\
124
+
> `const`_BlockExpression_
125
+
126
+
A *const block* is a variant of a block expression which evaluates in the compile time instead of in the run time.
127
+
128
+
A `const` block allows you to define a constant value without having to define a new `const` item, and thus is also sometimes called as inline `const` or anonymous `const`.
129
+
130
+
For example, this code:
131
+
132
+
```rust
133
+
# fnfoo(x:i32) ->i32 { x }
134
+
fnmain() {
135
+
letx=foo(const { 1+1 });
136
+
}
137
+
```
138
+
139
+
is equivalent to:
140
+
141
+
```rust
142
+
# fnfoo(x:i32) ->i32 { x }
143
+
fnmain() {
144
+
constFOO:i32=1+1;
145
+
letx=foo(FOO);
146
+
}
147
+
```
148
+
149
+
A `const` block supports type inference so you do not have to write the type of the constant value like a `const` item. `const` blocks are allowed to reference general parameters in their scope.
0 commit comments