Skip to content

Commit 6885c73

Browse files
committed
document raw string literals in tutorial.md and rust.md
1 parent 904c6c4 commit 6885c73

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

doc/rust.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,14 @@ literal : string_lit | char_lit | num_lit ;
239239

240240
~~~~~~~~ {.ebnf .gram}
241241
char_lit : '\x27' char_body '\x27' ;
242-
string_lit : '"' string_body * '"' ;
242+
string_lit : '"' string_body * '"' | 'r' raw_string ;
243243
244244
char_body : non_single_quote
245245
| '\x5c' [ '\x27' | common_escape ] ;
246246
247247
string_body : non_double_quote
248248
| '\x5c' [ '\x22' | common_escape ] ;
249+
raw_string : '"' raw_string_body '"' | '#' raw_string '#' ;
249250
250251
common_escape : '\x5c'
251252
| 'n' | 'r' | 't' | '0'
@@ -267,9 +268,10 @@ which must be _escaped_ by a preceding U+005C character (`\`).
267268

268269
A _string literal_ is a sequence of any Unicode characters enclosed within
269270
two `U+0022` (double-quote) characters, with the exception of `U+0022`
270-
itself, which must be _escaped_ by a preceding `U+005C` character (`\`).
271+
itself, which must be _escaped_ by a preceding `U+005C` character (`\`),
272+
or a _raw string literal_.
271273

272-
Some additional _escapes_ are available in either character or string
274+
Some additional _escapes_ are available in either character or non-raw string
273275
literals. An escape starts with a `U+005C` (`\`) and continues with one of
274276
the following forms:
275277

@@ -285,9 +287,35 @@ the following forms:
285287
* A _whitespace escape_ is one of the characters `U+006E` (`n`), `U+0072`
286288
(`r`), or `U+0074` (`t`), denoting the unicode values `U+000A` (LF),
287289
`U+000D` (CR) or `U+0009` (HT) respectively.
288-
* The _backslash escape_ is the character U+005C (`\`) which must be
290+
* The _backslash escape_ is the character `U+005C` (`\`) which must be
289291
escaped in order to denote *itself*.
290292

293+
Raw string literals do not process any escapes. They start with the character
294+
`U+0072` (`r`), followed zero or more of the character `U+0023` (`#`) and a
295+
`U+0022` (double-quote) character. The _raw string body_ is not defined in the
296+
EBNF grammar above: it can contain any sequence of Unicode characters and is
297+
terminated only by another `U+0022` (double-quote) character, followed by the
298+
same number of `U+0023` (`#`) characters that preceeded the opening `U+0022`
299+
(double-quote) character.
300+
301+
All Unicode characters contained in the raw string body represent themselves,
302+
the characters `U+0022` (double-quote) (except when followed by at least as
303+
many `U+0023` (`#`) characters as were used to start the raw string literal) or
304+
`U+005C` (`\`) do not have any special meaning.
305+
306+
Examples for string literals:
307+
308+
~~~
309+
"foo"; r"foo"; // foo
310+
"\"foo\""; r#""foo""#; // "foo"
311+
312+
"foo #\"# bar";
313+
r##"foo #"# bar"##; // foo #"# bar
314+
315+
"\x52"; "R"; r"R"; // R
316+
"\\x52"; r"\x52"; // \x52
317+
~~~
318+
291319
#### Number literals
292320

293321
~~~~~~~~ {.ebnf .gram}

doc/tutorial.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,12 @@ whose literals are written between single quotes, as in `'x'`.
353353
Just like C, Rust understands a number of character escapes, using the backslash
354354
character, such as `\n`, `\r`, and `\t`. String literals,
355355
written between double quotes, allow the same escape sequences.
356-
More on strings [later](#vectors-and-strings).
356+
357+
On the other hand, raw string literals do not process any escape sequences.
358+
They are written as `r##"blah"##`, with a matching number of zero or more `#`
359+
before the opening and after the closing quote, and can contain any sequence of
360+
characters except their closing delimiter. More on strings
361+
[later](#vectors-and-strings).
357362

358363
The nil type, written `()`, has a single value, also written `()`.
359364

0 commit comments

Comments
 (0)