You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/expressions/literal-expr.md
+212-5
Original file line number
Diff line number
Diff line change
@@ -26,29 +26,227 @@ Each of the lexical [literal][literal tokens] forms described earlier can make u
26
26
5; // integer type
27
27
```
28
28
29
+
In the descriptions below, the _string representation_ of a token is the sequence of characters from the input which matched the token's production in a *Lexer* grammar snippet.
30
+
31
+
> **Note**: this string representation never includes a character `U+000D` (CR) immediately followed by `U+000A` (LF): this pair would have been previously transformed into a single `U+000A` (LF).
32
+
33
+
## Escapes
34
+
35
+
The descriptions of textual literal expressions below make use of several forms of _escape_.
36
+
37
+
Each form of escape is characterised by:
38
+
* an _escape sequence_: a sequence of characters, which always begins with `U+005C` (`\`)
39
+
* an _escaped value_: either a single character or an empty sequence of characters
40
+
41
+
In the definitions of escapes below:
42
+
* An _octal digit_ is any of the characters in the range \[`0`-`7`].
43
+
* A _hexadecimal digit_ is any of the characters in the ranges \[`0`-`9`], \[`a`-`f`], or \[`A`-`F`].
44
+
45
+
### Simple escapes
46
+
47
+
Each sequence of characters occurring in the first column of the following table is an escape sequence.
48
+
49
+
In each case, the escaped value is the character given in the corresponding entry in the second column.
50
+
51
+
| Escape sequence | Escaped value |
52
+
|-----------------|--------------------------|
53
+
|`\0`| U+0000 (NUL) |
54
+
|`\t`| U+0009 (HT) |
55
+
|`\n`| U+000A (LF) |
56
+
|`\r`| U+000D (CR) |
57
+
|`\"`| U+0022 (QUOTATION MARK) |
58
+
|`\'`| U+0027 (APOSTROPHE) |
59
+
|`\\`| U+005C (REVERSE SOLIDUS) |
60
+
61
+
### 8-bit escapes
62
+
63
+
The escape sequence consists of `\x` followed by two hexadecimal digits.
64
+
65
+
The escaped value is the character whose [Unicode scalar value] is the result of interpreting the final two characters in the escape sequence as a hexadecimal integer, as if by [`u8::from_str_radix`] with radix 16.
66
+
67
+
> **Note**: the escaped value therefore has a [Unicode scalar value] in the range of [`u8`][numeric types].
68
+
69
+
### 7-bit escapes
70
+
71
+
The escape sequence consists of `\x` followed by an octal digit then a hexadecimal digit.
72
+
73
+
The escaped value is the character whose [Unicode scalar value] is the result of interpreting the final two characters in the escape sequence as a hexadecimal integer, as if by [`u8::from_str_radix`] with radix 16.
74
+
75
+
### Unicode escapes
76
+
77
+
The escape sequence consists of `\u{`, followed by a sequence of characters each of which is a hexadecimal digit or `_`, followed by `}`.
78
+
79
+
The escaped value is the character whose [Unicode scalar value] is the result of interpreting the hexadecimal digits contained in the escape sequence as a hexadecimal integer, as if by [`u8::from_str_radix`] with radix 16.
80
+
81
+
> **Note**: the permitted forms of a [CHAR_LITERAL] or [STRING_LITERAL] token ensure that there is such a character.
82
+
83
+
### String continuation escapes
84
+
85
+
The escape sequence consists of `\` followed immediately by `U+000A` (LF), and all following whitespace characters before the next non-whitespace character.
86
+
For this purpose, the whitespace characters are `U+0009` (HT), `U+000A` (LF), `U+000D` (CR), and `U+0020` (SPACE).
87
+
88
+
The escaped value is an empty sequence of characters.
89
+
90
+
> **Note**: The effect of this form of escape is that a string continuation skips following whitespace, including additional newlines.
Thetoken's_literalcontent_isthesequenceofcharactersfollowingthefirst `U+0027` (`'`) and preceding the last `U+0027` (`'`) inthestringrepresentationofthetoken.
A string literal expression consists of a single [STRING_LITERAL] or [RAW_STRING_LITERAL] token.
38
144
39
-
> **Note**: This section is incomplete.
145
+
The expression's type is a shared reference (with `static` lifetime) to the primitive [`str`][textual types] type.
146
+
That is, the type is `&'static str`.
147
+
148
+
The token must not have a suffix.
149
+
150
+
The token's _literal content_ is the sequence of characters following the first `U+0022` (`"`) and preceding the last `U+0022` (`"`) in the string representation of the token.
151
+
152
+
The literal expression's _represented string_ is a sequence of characters derived from the literal content as follows:
153
+
154
+
* If the token is a [STRING_LITERAL], each escape sequence of any of the following forms occurring in the literal content is replaced by the escape sequence's escaped value.
155
+
*[Simple escapes]
156
+
*[7-bit escapes]
157
+
*[Unicode escapes]
158
+
*[String continuation escapes]
159
+
160
+
These replacements take place in left-to-right order.
161
+
For example, the token `"\\x41"` is converted to the characters `\``x``4``1`.
162
+
163
+
* If the token is a [RAW_STRING_LITERAL], the represented string is identical to the literal content.
164
+
165
+
The expression's value is a reference to a statically allocated [`str`][textual types] containing the UTF-8 encoding of the represented string.
166
+
167
+
Examples of string literal expressions:
168
+
169
+
```rust
170
+
"foo"; r"foo"; // foo
171
+
"\"foo\""; r#""foo""#; // "foo"
172
+
173
+
"foo #\"# bar";
174
+
r##"foo #"# bar"##; // foo #"# bar
175
+
176
+
"\x52"; "R"; r"R"; // R
177
+
"\\x52"; r"\x52"; // \x52
178
+
```
40
179
41
180
## Byte literal expressions
42
181
43
182
A byte literal expression consists of a single [BYTE_LITERAL] token.
44
183
45
-
> **Note**: This section is incomplete.
184
+
The expression's type is the primitive [`u8`][numeric types] type.
185
+
186
+
The token must not have a suffix.
187
+
188
+
The token's _literal content_ is the sequence of characters following the first `U+0027` (`'`) and preceding the last `U+0027` (`'`) in the string representation of the token.
189
+
190
+
The literal expression's _represented character_ is derived from the literal content as follows:
191
+
192
+
* If the literal content is one of the following forms of escape sequence, the represented character is the escape sequence's escaped value:
193
+
*[Simple escapes]
194
+
*[8-bit escapes]
195
+
196
+
* Otherwise the represented character is the single character that makes up the literal content.
197
+
198
+
The expression's value is the represented character's [Unicode scalar value].
199
+
200
+
> **Note**: the permitted forms of a [BYTE_LITERAL] token ensure that these rules always produce a single character, whose Unicode scalar value is in the range of [`u8`][numeric types].
201
+
202
+
Examples of byte literal expressions:
203
+
204
+
```rust
205
+
b'R'; // 82
206
+
b'\''; // 39
207
+
b'\x52'; // 82
208
+
b'\xA0'; // 160
209
+
```
46
210
47
211
## Byte string literal expressions
48
212
49
-
A string literal expression consists of a single [BYTE_STRING_LITERAL] or [RAW_BYTE_STRING_LITERAL] token.
213
+
A byte string literal expression consists of a single [BYTE_STRING_LITERAL] or [RAW_BYTE_STRING_LITERAL] token.
50
214
51
-
> **Note**: This section is incomplete.
215
+
The expression's type is a shared reference (with `static` lifetime) to an array whose element type is [`u8`][numeric types].
216
+
That is, the type is `&'static [u8; N]`, where `N` is the number of bytes in the represented string described below.
217
+
218
+
The token must not have a suffix.
219
+
220
+
The token's _literal content_ is the sequence of characters following the first `U+0022` (`"`) and preceding the last `U+0022` (`"`) in the string representation of the token.
221
+
222
+
The literal expression's _represented string_ is a sequence of characters derived from the literal content as follows:
223
+
224
+
* If the token is a [BYTE_STRING_LITERAL], each escape sequence of any of the following forms occurring in the literal content is replaced by the escape sequence's escaped value.
225
+
*[Simple escapes]
226
+
*[8-bit escapes]
227
+
*[String continuation escapes]
228
+
229
+
These replacements take place in left-to-right order.
230
+
For example, the token `b"\\x41"` is converted to the characters `\``x``4``1`.
231
+
232
+
* If the token is a [RAW_BYTE_STRING_LITERAL], the represented string is identical to the literal content.
233
+
234
+
The expression's value is a reference to a statically allocated array containing the [Unicode scalar values] of the characters in the represented string, in the same order.
235
+
236
+
> **Note**: the permitted forms of [BYTE_STRING_LITERAL] and [RAW_BYTE_STRING_LITERAL] tokens ensure that these rules always produce array element values in the range of [`u8`][numeric types].
237
+
238
+
Examples of byte string literal expressions:
239
+
240
+
```rust
241
+
b"foo"; br"foo"; // foo
242
+
b"\"foo\""; br#""foo""#; // "foo"
243
+
244
+
b"foo #\"# bar";
245
+
br##"foo #"# bar"##; // foo #"# bar
246
+
247
+
b"\x52"; b"R"; br"R"; // R
248
+
b"\\x52"; br"\x52"; // \x52
249
+
```
52
250
53
251
## C string literal expressions
54
252
@@ -167,6 +365,11 @@ The expression's type is the primitive [boolean type], and its value is:
Copy file name to clipboardExpand all lines: src/tokens.md
+11-25
Original file line number
Diff line number
Diff line change
@@ -156,30 +156,13 @@ A _string literal_ is a sequence of any Unicode characters enclosed within two
156
156
`U+0022` (double-quote) characters, with the exception of `U+0022` itself,
157
157
which must be _escaped_ by a preceding `U+005C` character (`\`).
158
158
159
-
Line-breaks are allowed in string literals. A line-break is either a newline
160
-
(`U+000A`) or a pair of carriage return and newline (`U+000D`, `U+000A`). Both
161
-
byte sequences are normally translated to `U+000A`, but as a special exception,
162
-
when an unescaped `U+005C` character (`\`) occurs immediately before a line
163
-
break, then the line break character(s), and all immediately following
164
-
`` (`U+0020`), `\t` (`U+0009`), `\n` (`U+000A`) and `\r` (`U+0000D`) characters
165
-
are ignored. Thus `a`, `b` and `c` are equal:
159
+
Line-breaks are allowed in string literals.
160
+
A line-break is either a newline (`U+000A`) or a pair of carriage return and newline (`U+000D`, `U+000A`).
161
+
Both byte sequences are translated to `U+000A`.
166
162
167
-
```rust
168
-
leta="foobar";
169
-
letb="foo\
170
-
bar";
171
-
letc="foo\
172
-
173
-
bar";
174
-
175
-
assert_eq!(a, b);
176
-
assert_eq!(b, c);
177
-
```
163
+
When an unescaped `U+005C` character (`\`) occurs immediately before a line break, the line break does not appear in the string represented by the token.
164
+
See [String continuation escapes] for details.
178
165
179
-
> Note: Rust skipping additional newlines (like in example `c`) is potentially confusing and
180
-
> unexpected. This behavior may be adjusted in the future. Until a decision is made, it is
181
-
> recommended to avoid relying on this, i.e. skipping multiple newlines with line continuations.
182
-
> See [this issue](https://github.com/rust-lang/reference/pull/1042) for more information.
183
166
184
167
#### Character escapes
185
168
@@ -274,7 +257,7 @@ preceded by the characters `U+0062` (`b`) and `U+0022` (double-quote), and
274
257
followed by the character `U+0022`. If the character `U+0022` is present within
275
258
the literal, it must be _escaped_ by a preceding `U+005C` (`\`) character.
276
259
Alternatively, a byte string literal can be a _raw byte string literal_, defined
277
-
below. The type of a byte string literal of length `n` is `&'static [u8; n]`.
260
+
below.
278
261
279
262
Some additional _escapes_ are available in either byte or non-raw byte string
280
263
literals. An escape starts with a `U+005C` (`\`) and continues with one of the
@@ -479,7 +462,7 @@ An _integer literal_ has one of four forms:
479
462
480
463
Like any literal, an integer literal may be followed (immediately, without any spaces) by a suffix as described above.
481
464
The suffix may not begin with `e` or `E`, as that would be interpreted as the exponent of a floating-point literal.
482
-
See [literal expressions] for the effect of these suffixes.
465
+
See [Integer literal expressions] for the effect of these suffixes.
483
466
484
467
Examples of integer literals which are accepted as literal expressions:
485
468
@@ -576,7 +559,7 @@ A _floating-point literal_ has one of two forms:
576
559
Like integer literals, a floating-point literal may be followed by a
577
560
suffix, so long as the pre-suffix part does not end with `U+002E` (`.`).
578
561
The suffix may not begin with `e` or `E` if the literal does not include an exponent.
579
-
See [literal expressions] for the effect of these suffixes.
562
+
See [Floating-point literal expressions] for the effect of these suffixes.
580
563
581
564
Examples of floating-point literals which are accepted as literal expressions:
582
565
@@ -784,12 +767,14 @@ Similarly the `r`, `b`, `br`, `c`, and `cr` prefixes used in raw string literals
0 commit comments