Skip to content

Commit

Permalink
add more docs for escape sequence and string (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoorkin authored Apr 17, 2024
1 parent 6253d3a commit 5c72865
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 29 deletions.
64 changes: 50 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,6 @@ let d = a || b
let e = not(a)
```

### Byte

A byte literal in MoonBit is either a single ASCII character or a single escape enclosed in single quotes `'`, and preceded by the character `b`. Byte literals are of type `Byte`. For example:

```rust
fn init {
let b1 = b'a'
println(b1.to_int())
let b2 = b'\xff'
println(b2.to_int())
}
```

### Number

MoonBit have integer type and floating point type:
Expand Down Expand Up @@ -306,7 +293,33 @@ let another_hex = 0xA

### String

String interpolation is a powerful feature in MoonBit that enables you to substitute variables within interpolated strings. This feature simplifies the process of constructing dynamic strings by directly embedding variable values into the text.
`String` holds a sequence of UTF-16 code units. You can use double quotes to create a string, or use `#|` to write a multi-line string.

```rust
let a = "兔rabbit"
println(a[0]) // output: 兔
println(a[1]) // output: r
```

```rust
let b =
#| Hello
#| MoonBit
#|
```

In double quotes string, a backslash followed by certain special characters forms an escape sequence:

|escape sequences|description|
|-|-|
|`\n`,`\r`,`\t`,`\b`|New line, Carriage return, Horizontal tab, Backspace|
|`\\`|Backslash|
|`\x41`|Hexadecimal escape sequence|
|`\o102`|Octal escape sequence|
|`\u5154`,`\u{1F600}`|Unicode escape sequence|


MoonBit supports string interpolation. It enables you to substitute variables within interpolated strings. This feature simplifies the process of constructing dynamic strings by directly embedding variable values into the text.

```swift live
fn init {
Expand All @@ -317,6 +330,29 @@ fn init {

Variables used for string interpolation must support the `to_string` method.

### Char

`Char` is an integer representing a Unicode code point.

```rust
let a : Char = 'A'
let b = '\x41'
let c = '🐰'
```

### Byte

A byte literal in MoonBit is either a single ASCII character or a single escape enclosed in single quotes `'`, and preceded by the character `b`. Byte literals are of type `Byte`. For example:

```rust
fn init {
let b1 : Byte = b'a'
println(b1.to_int())
let b2 = b'\xff'
println(b2.to_int())
}
```

### Tuple

A tuple is a collection of finite values constructed using round brackets `()` with the elements separated by commas `,`. The order of elements matters; for example, `(1,true)` and `(true,1)` have different types. Here's an example:
Expand Down
66 changes: 51 additions & 15 deletions zh-docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,6 @@ let d = a || b
let e = not(a)
```

### 字节

在MoonBit中,字节字面量可以是一个ASCII字符或一个转义序列,它们被单引号'包围,并且前面有字符b。字节字面量的类型是Byte。例如:

```rust
fn init {
let b1 = b'a'
println(b1.to_int())
let b2 = b'\xff'
println(b2.to_int())
}
```

### 数字

MoonBit 支持整型和浮点类型:
Expand Down Expand Up @@ -307,8 +294,34 @@ let another_hex = 0xA

### 字符串

字符串内插是 MoonBit 中的一个强大功能,它可以将字符串中的内插变量替换为具体值。
该功能通过将变量值直接嵌入到文本中来简化构建动态字符串的过程。
字符串`String`内部保存了UTF-16编码单元序列。可以使用双引号来表示一个字符串,或者通过`#|`来书写多行字符串。

```rust
let a = "兔rabbit"
println(a[0]) // output: 兔
println(a[1]) // output: r
```

```rust
let b =
#| Hello
#| MoonBit
#|
```

在双引号包围的字符串之间支持使用`\`表示特殊字符转义:

|转义序列|解释|
|-|-|
|`\n`,`\r`,`\t`,`\b`|换行、回车、水平制表符、退格|
|`\\`|反斜杠|
|`\x41`|16进制转义序列|
|`\o102`|8进制转义序列|
|`\u5154`,`\u{1F600}`|Unicode字符转义序列|


MoonBit支持字符串插值,它可以把字符串中内插的变量替换为变量具体的值。
这个特性能够简化动态拼接字符串的过程。

```rust live
fn init {
Expand All @@ -319,6 +332,29 @@ fn init {

用于字符串内插的变量必须支持 `to_string` 方法。

### 字符

字符`Char`是表示一个Unicode字符的整数。

```rust
let a : Char = 'A'
let b = '\x41'
let c = '🐰'
```

### 字节

在MoonBit中,字节字面量可以是一个ASCII字符或一个转义序列,它们被单引号`'`包围,并且前面有字符`b`。字节字面量的类型是Byte。例如:

```rust
fn init {
let b1 : Byte = b'a'
println(b1.to_int())
let b2 = b'\xff'
println(b2.to_int())
}
```

### 元组

元组是一个有限值的有序集合,使用圆括号 `()` 构造,其中的元素由逗号 `,` 分隔。
Expand Down

0 comments on commit 5c72865

Please sign in to comment.