@@ -6,7 +6,7 @@ Expanded in many releases, see each aspect below for more details.
6
6
7
7
A ` const fn ` allows you to execute code in a "const context." For example:
8
8
9
- ```
9
+ ``` rust
10
10
const fn five () -> i32 {
11
11
5
12
12
}
@@ -34,7 +34,7 @@ that it is becoming more `const` over time.
34
34
35
35
You can do arithmetic on integer literals:
36
36
37
- ```
37
+ ``` rust
38
38
const fn foo () -> i32 {
39
39
5 + 6
40
40
}
@@ -46,7 +46,7 @@ const fn foo() -> i32 {
46
46
47
47
You can use boolean operators other than ` && ` and ` || ` , because they short-circut evaluation:
48
48
49
- ```
49
+ ``` rust
50
50
const fn mask (val : u8 ) -> u8 {
51
51
let mask = 0x0f ;
52
52
@@ -60,7 +60,7 @@ const fn mask(val: u8) -> u8 {
60
60
61
61
You can create arrays, structs, enums, and tuples:
62
62
63
- ```
63
+ ``` rust
64
64
struct Point {
65
65
x : i32 ,
66
66
y : i32 ,
@@ -92,7 +92,7 @@ const fn foo() {
92
92
You can call ` const fn ` from a ` const fn ` :
93
93
94
94
95
- ```
95
+ ``` rust
96
96
const fn foo () -> i32 {
97
97
5
98
98
}
@@ -108,7 +108,7 @@ const fn bar() -> i32 {
108
108
109
109
You can index into an array or slice:
110
110
111
- ```
111
+ ``` rust
112
112
const fn foo () -> i32 {
113
113
let array = [1 , 2 , 3 ];
114
114
@@ -122,7 +122,7 @@ const fn foo() -> i32 {
122
122
123
123
You can access parts of a struct or tuple:
124
124
125
- ```
125
+ ``` rust
126
126
struct Point {
127
127
x : i32 ,
128
128
y : i32 ,
@@ -147,7 +147,7 @@ const fn foo() {
147
147
148
148
You can read from a constant:
149
149
150
- ```
150
+ ``` rust
151
151
const FOO : i32 = 5 ;
152
152
153
153
const fn foo () -> i32 {
@@ -163,7 +163,7 @@ Note that this is *only* `const`, not `static`.
163
163
164
164
You can create and de-reference references:
165
165
166
- ```
166
+ ``` rust
167
167
const fn foo (r : & i32 ) {
168
168
* r ;
169
169
@@ -177,7 +177,7 @@ const fn foo(r: &i32) {
177
177
178
178
You may cast things, except for raw pointers may not be casted to an integer:
179
179
180
- ```
180
+ ``` rust
181
181
const fn foo () {
182
182
let x : usize = 5 ;
183
183
@@ -191,7 +191,7 @@ const fn foo() {
191
191
192
192
You can use irrefutable patterns that destructure values. For example:
193
193
194
- ```
194
+ ``` rust
195
195
const fn foo ((x , y ): (u8 , u8 )) {
196
196
// ...
197
197
}
@@ -206,7 +206,7 @@ place that uses irrefutable patterns.
206
206
207
207
You can use both mutable and immutable ` let ` bindings:
208
208
209
- ```
209
+ ``` rust
210
210
const fn foo () {
211
211
let x = 5 ;
212
212
let mut y = 10 ;
@@ -219,7 +219,7 @@ const fn foo() {
219
219
220
220
You can use assignment and assignment operators:
221
221
222
- ```
222
+ ``` rust
223
223
const fn foo () {
224
224
let mut x = 5 ;
225
225
x = 10 ;
@@ -232,7 +232,7 @@ const fn foo() {
232
232
233
233
You can call an ` unsafe fn ` inside a ` const fn ` :
234
234
235
- ```
235
+ ``` rust
236
236
const unsafe fn foo () -> i32 { 5 }
237
237
238
238
const fn bar () -> i32 {
0 commit comments