Skip to content

Commit ea0c603

Browse files
committed
Constants
1 parent 7ccc9b2 commit ea0c603

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/constants.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,37 @@ slug: /constants
33
---
44

55
# Constants
6+
7+
Constants allow you to write a value a single time, give it a name, and then reuse it throughout your program.
8+
9+
They are similar to [Bindings](/docs/bindings.md), which the primary difference being that they are defined at the top level of the program and are order independent. They will live for and be reused for the entire lifecycle of the `main` function.
10+
11+
As a simple example, you might want to reuse a string multiple times:
12+
13+
```rue
14+
const GREET: Bytes = "Hello, ";
15+
16+
fn main(person_a: Bytes, person_b: Bytes) -> (Bytes, Bytes) {
17+
(GREET + person_a, GREET + person_b)
18+
}
19+
```
20+
21+
## Inline Constants
22+
23+
You can mark a constant as `inline`, which forces its value to be inserted everywhere it's referenced rather than being defined a single time and reused.
24+
25+
As an example, this will behave the same as if you had written the string multiple times:
26+
27+
```rue
28+
inline const GREET: Bytes = "Hello, ";
29+
30+
fn main(person_a: Bytes, person_b: Bytes) -> (Bytes, Bytes) {
31+
(GREET + person_a, GREET + person_b)
32+
}
33+
```
34+
35+
This should be used sparingly, since it can come with either a performance hit, an increase in compiled output size, or both.
36+
37+
:::tip
38+
The compiler will automatically inline constants if they are only referenced a single time. You almost never need to explicitly mark them as `inline`.
39+
:::

0 commit comments

Comments
 (0)