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
+49-1Lines changed: 49 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -252,7 +252,48 @@ b"\\x52"; br"\x52"; // \x52
252
252
253
253
A C string literal expression consists of a single [C_STRING_LITERAL] or [RAW_C_STRING_LITERAL] token.
254
254
255
-
> **Note**: This section is incomplete.
255
+
The expression's type is a shared reference (with `static` lifetime) to the standard library [CStr] type.
256
+
That is, the type is `&'static core::ffi::CStr`.
257
+
258
+
The token must not have a suffix.
259
+
260
+
The token's _literal content_ is the sequence of characters following the first `"` and preceding the last `"` in the string representation of the token.
261
+
262
+
The literal expression's _represented bytes_ are a sequence of bytes derived from the literal content as follows:
263
+
264
+
* If the token is a [C_STRING_LITERAL], the literal content is treated as a sequence of items, each of which is either a single Unicode character other than `\` or an [escape].
265
+
The sequence of items is converted to a sequence of bytes as follows:
266
+
* Each single Unicode character contributes its UTF-8 representation.
267
+
* Each [simple escape] contributes the [Unicode scalar value] of its escaped value.
268
+
* Each [8-bit escape] contributes a single byte containing the [Unicode scalar value] of its escaped value.
269
+
* Each [unicode escape] contributes the UTF-8 representation of its escaped value.
270
+
* Each [string continuation escape] contributes no bytes.
271
+
272
+
* If the token is a [RAW_C_STRING_LITERAL], the represented bytes are the UTF-8 encoding of the literal content.
273
+
274
+
> **Note**: the permitted forms of [C_STRING_LITERAL] and [RAW_C_STRING_LITERAL] tokens ensure that the represented bytes never include a null byte.
275
+
276
+
The expression's value is a reference to a statically allocated [CStr] whose array of bytes contains the represented bytes followed by a null byte.
277
+
278
+
Examples of C string literal expressions:
279
+
280
+
```rust
281
+
c"foo"; cr"foo"; // foo
282
+
c"\"foo\""; cr#""foo""#; // "foo"
283
+
284
+
c"foo #\"# bar";
285
+
cr##"foo #"# bar"##; // foo #"# bar
286
+
287
+
c"\x52"; c"R"; cr"R"; // R
288
+
c"\\x52"; cr"\x52"; // \x52
289
+
290
+
c"æ"; // LATIN SMALL LETTER AE (U+00E6)
291
+
c"\u{00E6}"; // LATIN SMALL LETTER AE (U+00E6)
292
+
c"\xC3\xA6"; // LATIN SMALL LETTER AE (U+00E6)
293
+
294
+
c"\xE6".to_bytes(); // [230]
295
+
c"\u{00E6}".to_bytes(); // [195, 166]
296
+
```
256
297
257
298
## Integer literal expressions
258
299
@@ -365,13 +406,20 @@ The expression's type is the primitive [boolean type], and its value is:
0 commit comments