|
| 1 | +# `const fn` |
| 2 | + |
| 3 | +Initially added:  |
| 4 | + |
| 5 | +Expanded in many releases, see each aspect below for more details. |
| 6 | + |
| 7 | +A `const fn` allows you to execute code in a "const context." For example: |
| 8 | + |
| 9 | +``` |
| 10 | +const fn five() -> i32 { |
| 11 | + 5 |
| 12 | +} |
| 13 | +
|
| 14 | +const FIVE: i32 = five(); |
| 15 | +``` |
| 16 | + |
| 17 | +You cannot execute arbitrary code; the reasons why boil down to "you can |
| 18 | +destroy the type system." The details are a bit too much to put here, but the |
| 19 | +core idea is that `const fn` started off allowing the absolutely minimal |
| 20 | +subset of the language, and has slowly added more abilities over time. |
| 21 | +Therefore, while you can create a `const fn` in Rust 1.31, you cannot do much |
| 22 | +with it. This is why we didn't add `const fn` to the Rust 2018 section; it |
| 23 | +truly didn't become useful until after the release of the 2018 edition. This |
| 24 | +means that if you read this document top to bottom, the earlier versions may |
| 25 | +describe restrictions that are relaxed in later versions. |
| 26 | + |
| 27 | +Additionally, this has allowed more and more of the standard library to be |
| 28 | +made `const`, we won't put all of those changes here, but you should know |
| 29 | +that it is becoming more `const` over time. |
| 30 | + |
| 31 | +## Arithmetic and comparison operators on integers |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +You can do arithmetic on integer literals: |
| 36 | + |
| 37 | +``` |
| 38 | +const fn foo() -> i32 { |
| 39 | + 5 + 6 |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Many boolean operators |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +You can use boolean operators other than `&&` and `||`, because they short-circut evaluation: |
| 48 | + |
| 49 | +``` |
| 50 | +const fn mask(val: u8) -> u8 { |
| 51 | + let mask = 0x0f; |
| 52 | +
|
| 53 | + mask & val |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Constructing arrays, structs, enums, and tuples |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +You can create arrays, structs, enums, and tuples: |
| 62 | + |
| 63 | +``` |
| 64 | +struct Point { |
| 65 | + x: i32, |
| 66 | + y: i32, |
| 67 | +} |
| 68 | +
|
| 69 | +enum Error { |
| 70 | + Incorrect, |
| 71 | + FileNotFound, |
| 72 | +} |
| 73 | +
|
| 74 | +const fn foo() { |
| 75 | + let array = [1, 2, 3]; |
| 76 | +
|
| 77 | + let point = Point { |
| 78 | + x: 5, |
| 79 | + y: 10, |
| 80 | + }; |
| 81 | +
|
| 82 | + let error = Error::FileNotFound; |
| 83 | +
|
| 84 | + let tuple = (1, 2, 3); |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +## Calls to other const fns |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +You can call `const fn` from a `const fn`: |
| 93 | + |
| 94 | + |
| 95 | +``` |
| 96 | +const fn foo() -> i32 { |
| 97 | + 5 |
| 98 | +} |
| 99 | +
|
| 100 | +const fn bar() -> i32 { |
| 101 | + foo() |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## Index expressions on arrays and slices |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +You can index into an array or slice: |
| 110 | + |
| 111 | +``` |
| 112 | +const fn foo() -> i32 { |
| 113 | + let array = [1, 2, 3]; |
| 114 | +
|
| 115 | + array[1] |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Field accesses on structs and tuples |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +You can access parts of a struct or tuple: |
| 124 | + |
| 125 | +``` |
| 126 | +struct Point { |
| 127 | + x: i32, |
| 128 | + y: i32, |
| 129 | +} |
| 130 | +
|
| 131 | +const fn foo() { |
| 132 | + let point = Point { |
| 133 | + x: 5, |
| 134 | + y: 10, |
| 135 | + }; |
| 136 | +
|
| 137 | + let tuple = (1, 2, 3); |
| 138 | +
|
| 139 | + point.x; |
| 140 | + tuple.0; |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +## Reading from constants |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +You can read from a constant: |
| 149 | + |
| 150 | +``` |
| 151 | +const FOO: i32 = 5; |
| 152 | +
|
| 153 | +const fn foo() -> i32 { |
| 154 | + FOO |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +Note that this is *only* `const`, not `static`. |
| 159 | + |
| 160 | +## & and * of references |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +You can create and de-reference references: |
| 165 | + |
| 166 | +``` |
| 167 | +const fn foo(r: &i32) { |
| 168 | + *r; |
| 169 | +
|
| 170 | + &5; |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +## Casts, except for raw pointer to integer casts |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | +You may cast things, except for raw pointers may not be casted to an integer: |
| 179 | + |
| 180 | +``` |
| 181 | +const fn foo() { |
| 182 | + let x: usize = 5; |
| 183 | +
|
| 184 | + x as i32; |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +## Irrefutable destructuring patterns |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | +You can use irrefutable patterns that destructure values. For example: |
| 193 | + |
| 194 | +``` |
| 195 | +const fn foo((x, y): (u8, u8)) { |
| 196 | + // ... |
| 197 | + # unimplemented!(); |
| 198 | +} |
| 199 | +``` |
| 200 | + |
| 201 | +Here, `foo` destructures the tuple into `x` and `y`. `if let` is another |
| 202 | +place that uses irrefutable patterns. |
| 203 | + |
| 204 | +## `let` bindings |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | +You can use both mutable and immutable `let` bindings: |
| 209 | + |
| 210 | +``` |
| 211 | +const fn foo() { |
| 212 | + let x = 5; |
| 213 | + let mut y = 10; |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +## Assignment |
| 218 | + |
| 219 | + |
| 220 | + |
| 221 | +You can use assignment and assignment operators: |
| 222 | + |
| 223 | +``` |
| 224 | +const fn foo() { |
| 225 | + let mut x = 5; |
| 226 | + x = 10; |
| 227 | +} |
| 228 | +``` |
| 229 | + |
| 230 | +## Calling `unsafe fn` |
| 231 | + |
| 232 | + |
| 233 | + |
| 234 | +YOu can call an `unsafe fn` inside a `const fn`: |
| 235 | + |
| 236 | +``` |
| 237 | +const unsafe fn foo() -> i32 { 5 } |
| 238 | +
|
| 239 | +const fn bar() -> i32 { |
| 240 | + unsafe { foo() } |
| 241 | +} |
| 242 | +``` |
0 commit comments