Skip to content

Commit 106979e

Browse files
committed
Strings and destructuring
1 parent 93e6fdf commit 106979e

7 files changed

Lines changed: 71 additions & 28 deletions

File tree

docs/bindings.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,40 @@ This should be used sparingly, since it can come with either a performance hit,
4242
:::tip
4343
The compiler will automatically inline `let` bindings if they are only referenced a single time.
4444
:::
45+
46+
## Destructuring
47+
48+
You can destructure values (including `let` bindings and function parameters) into multiple bindings at once.
49+
50+
For example, if you have a pair, you can destructure the first and rest values into separate bindings:
51+
52+
```rue
53+
let (first, rest) = (100, 200);
54+
```
55+
56+
This also works for lists (as long as the values are known to not be nil):
57+
58+
```rue
59+
let [a, b, c] = [100, 200, 300];
60+
```
61+
62+
If you have a struct, you can destructure the fields:
63+
64+
```rue
65+
struct Point {
66+
x: Int,
67+
y: Int,
68+
}
69+
70+
let { x, y } = Point { x: 100, y: 200 };
71+
```
72+
73+
These destructuring patterns can be combined arbitrarily:
74+
75+
```rue
76+
let ([a, b, c], { x, y }) = ([100, 200, 300], Point { x: 400, y: 500 });
77+
```
78+
79+
:::note
80+
When you destructure a value, it's first assigned to a temporary binding, and then that binding is referenced by each of the bindings in the destructuring pattern. Thus, if you're destructuring a non-inline variable, it's best to mark the destructured bindings as `inline` to reduce the size of the compiled program.
81+
:::

docs/builtins.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ You can click on each of these types to go to their corresponding page and learn
1313
1. [**Atom**](/docs/type-system/atoms.md#atom)
1414
2. [**Bytes**](/docs/type-system/atoms.md#bytes)
1515
3. [**Bytes32**](/docs/type-system/atoms.md#bytes32)
16-
4. [**PublicKey**](/docs/type-system/atoms.md#publickey)
17-
5. [**Signature**](/docs/type-system/atoms.md#signature)
18-
6. [**K1PublicKey**](/docs/type-system/atoms.md#k1publickey)
19-
7. [**K1Signature**](/docs/type-system/atoms.md#k1signature)
20-
8. [**R1PublicKey**](/docs/type-system/atoms.md#r1publickey)
21-
9. [**R1Signature**](/docs/type-system/atoms.md#r1signature)
22-
10. [**Int**](/docs/type-system/atoms.md#int)
23-
11. [**Bool**](/docs/type-system/atoms.md#bool)
24-
12. [**Any**](/docs/type-system/pairs.md#any)
25-
13. [**List**](/docs/type-system/pairs.md#lists)
26-
14. [**AlternatingList**](/docs/type-system/pairs.md#alternating-lists)
16+
4. [**String**](/docs/type-system/atoms.md#string)
17+
5. [**PublicKey**](/docs/type-system/atoms.md#publickey)
18+
6. [**Signature**](/docs/type-system/atoms.md#signature)
19+
7. [**K1PublicKey**](/docs/type-system/atoms.md#k1publickey)
20+
8. [**K1Signature**](/docs/type-system/atoms.md#k1signature)
21+
9. [**R1PublicKey**](/docs/type-system/atoms.md#r1publickey)
22+
10. [**R1Signature**](/docs/type-system/atoms.md#r1signature)
23+
11. [**Int**](/docs/type-system/atoms.md#int)
24+
12. [**Bool**](/docs/type-system/atoms.md#bool)
25+
13. [**Any**](/docs/type-system/pairs.md#any)
26+
14. [**List**](/docs/type-system/pairs.md#lists)
27+
15. [**AlternatingList**](/docs/type-system/pairs.md#alternating-lists)
2728

2829
## Functions
2930

docs/constants.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ They are similar to [Bindings](/docs/bindings.md), which the primary difference
1111
As a simple example, you might want to reuse a string multiple times:
1212

1313
```rue
14-
const GREET: Bytes = "Hello, ";
14+
const GREET: String = "Hello, ";
1515
16-
fn main(person_a: Bytes, person_b: Bytes) -> (Bytes, Bytes) {
16+
fn main(person_a: String, person_b: String) -> (String, String) {
1717
(GREET + person_a, GREET + person_b)
1818
}
1919
```
@@ -25,9 +25,9 @@ You can mark a constant as `inline`, which forces its value to be inserted every
2525
As an example, this will behave the same as if you had written the string multiple times:
2626

2727
```rue
28-
inline const GREET: Bytes = "Hello, ";
28+
inline const GREET: String = "Hello, ";
2929
30-
fn main(person_a: Bytes, person_b: Bytes) -> (Bytes, Bytes) {
30+
fn main(person_a: String, person_b: String) -> (String, String) {
3131
(GREET + person_a, GREET + person_b)
3232
}
3333
```

docs/installation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ The simplest example of a program in Rue is the classic hello world example.
7171
Write the following program in a file named `hello.rue`:
7272

7373
```rue title="hello.rue"
74-
fn main() -> Bytes {
74+
fn main() -> String {
7575
"Hello, world!"
7676
}
7777
```

docs/type-system/atoms.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ There are multiple types of atoms:
1111
1. **Atom** - has no semantic meaning
1212
2. **Bytes** - explicitly treated as bytes
1313
3. **Bytes32** - a `Bytes` value with a length of 32
14-
4. **PublicKey** - a [BLS12-381](https://en.wikipedia.org/wiki/BLS_digital_signature) public key with a length of 48 bytes
15-
5. **Signature** - a [BLS12-381](https://en.wikipedia.org/wiki/BLS_digital_signature) signature with a length of 96 bytes
16-
6. **K1PublicKey** - a secp256k1 public key with a length of 33 bytes
17-
7. **K1Signature** - a secp256k1 signature with a length of 64 bytes
18-
8. **R1PublicKey** - a secp256r1 public key with a length of 33 bytes
19-
9. **R1Signature** - a secp256r1 signature with a length of 64 bytes
20-
10. **Int** - a signed integer of arbitrary size
21-
11. **Bool** - either `true` or `false`
14+
4. **String** - a UTF-8 encoded string
15+
5. **PublicKey** - a [BLS12-381](https://en.wikipedia.org/wiki/BLS_digital_signature) public key with a length of 48 bytes
16+
6. **Signature** - a [BLS12-381](https://en.wikipedia.org/wiki/BLS_digital_signature) signature with a length of 96 bytes
17+
7. **K1PublicKey** - a secp256k1 public key with a length of 33 bytes
18+
8. **K1Signature** - a secp256k1 signature with a length of 64 bytes
19+
9. **R1PublicKey** - a secp256r1 public key with a length of 33 bytes
20+
10. **R1Signature** - a secp256r1 signature with a length of 64 bytes
21+
11. **Int** - a signed integer of arbitrary size
22+
12. **Bool** - either `true` or `false`
2223

2324
## Atom
2425

@@ -56,6 +57,10 @@ This will emit code at runtime to check that the value has a length of 32.
5657
`Bytes32` can only guarantee the length of trusted values. See the [Security](/docs/security.md#untrusted-values) page for more details.
5758
:::
5859

60+
## String
61+
62+
The `String` type represents a UTF-8 encoded string. It's functionally equivalent to `Bytes`, but provides a more descriptive name for working with text values, and hints to the compiler that the value is a string rather than a sequence of bytes (which is useful for error messages).
63+
5964
## PublicKey
6065

6166
The `PublicKey` type represents a [BLS12-381](https://en.wikipedia.org/wiki/BLS_digital_signature) public key (also commonly referred to as a G1 element).

docs/type-system/structs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ While the type can technically be inferred, it's often necessary to explicitly u
7777
Default values are always inlined exactly as written. If the value is expensive to compute, or would require a lot of bytes to duplicate in the output program, it should be extracted out into a constant:
7878

7979
```rue
80-
const DEFAULT_NAME: Bytes = "Patricia";
80+
const DEFAULT_NAME: String = "Patricia";
8181
8282
struct Person {
83-
name: Bytes = DEFAULT_NAME,
83+
name: String = DEFAULT_NAME,
8484
}
8585
```
8686

src/theme/prism-rue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Prism.languages.rue = {
4141
alias: "keyword",
4242
},
4343
module: {
44-
pattern: /\b(?:import|mod)\b/,
44+
pattern: /\b(?:import|mod|super)\b/,
4545
alias: "keyword",
4646
},
4747
modifier: { pattern: /\b(?:inline|export|extern|test)\b/, alias: "keyword" },
@@ -51,7 +51,7 @@ Prism.languages.rue = {
5151
alias: "constant",
5252
},
5353
builtin:
54-
/\b(?:Atom|Bytes|Bytes32|PublicKey|Signature|K1PublicKey|K1Signature|R1PublicKey|R1Signature|Int|Bool|Any|List|AlternatingList)\b/,
54+
/\b(?:Atom|Bytes|String|Bytes32|PublicKey|Signature|K1PublicKey|K1Signature|R1PublicKey|R1Signature|Int|Bool|Any|List|AlternatingList)\b/,
5555
"class-name": /\b[A-Z][a-z][a-zA-Z0-9_]*\b/,
5656
constant: /\b[A-Z][A-Z0-9_]*\b/,
5757
};

0 commit comments

Comments
 (0)