String literals are constant data, embedded in the program, and thus stored inside the read-only memory zone.
Strings literals are written between quotes and can be used to initialize string variables:
let s = "abcdef";
s"abcdef"8.4.1Multi-lines definition¶
We have seen a string literal defined on one line, but it can also be defined on multiple lines (new line characters are included):
let s = "my string
on
multiple
lines.";
s"my string\non\nmultiple\nlines."Using the backslash (\) escape character, we can avoid the new line
characters:
let s = "my string \
on \
multiple \
lines.";
s"my string on multiple lines."8.4.2Using escape characters¶
The escape characters we have seen for the char type can be used inside a
literal string definition:
println!("my string\n \x3f \u{211d}");my string
? ℝ
The NULL character (character ASCII 0) is not interpreted as a
string terminator and is printed as-is:
println!("abc\0def");abc def
8.4.3Raw string¶
Using the r prefix we can define a raw string in which escape characters
are not interpreted (much like Python’s raw strings):
r"My string with any char\n \x3f.'abc'""My string with any char\\n \\x3f.'abc'"8.4.4Byte string¶
Byte string (slice of bytes (u8)):
let bytes = b"abc"; // Type is &[u8; 3]
bytes[97, 98, 99]