@@ -12,17 +12,18 @@ encoding of UTF-8 sequences. Additionally, unlike some systems languages,
12
12
strings are not null-terminated and can contain null bytes.
13
13
14
14
Rust has two main types of strings: ` &str ` and ` String ` . Let’s talk about
15
- ` &str ` first. These are called ‘string slices’. String literals are of the type
16
- ` &'static str ` :
15
+ ` &str ` first. These are called ‘string slices’. A string slice has a fixed
16
+ size, and cannot be mutated. It is a reference to a sequence of UTF-8 bytes.
17
17
18
18
``` rust
19
19
let greeting = " Hello there." ; // greeting: &'static str
20
20
```
21
21
22
- This string is statically allocated, meaning that it’s saved inside our
23
- compiled program, and exists for the entire duration it runs. The ` greeting `
24
- binding is a reference to this statically allocated string. String slices
25
- have a fixed size, and cannot be mutated.
22
+ ` "Hello there." ` is a string literal and its type is ` &'static str ` . A string
23
+ literal is a string slice that is statically allocated, meaning that it’s saved
24
+ inside our compiled program, and exists for the entire duration it runs. The
25
+ ` greeting ` binding is a reference to this statically allocated string. Any
26
+ function expecting a string slice will also accept a string literal.
26
27
27
28
String literals can span multiple lines. There are two forms. The first will
28
29
include the newline and the leading spaces:
@@ -34,7 +35,7 @@ let s = "foo
34
35
assert_eq! (" foo\ n bar" , s );
35
36
```
36
37
37
- The second, with a ` \ ` , does not trim the spaces :
38
+ The second, with a ` \ ` , trims the spaces and the newline :
38
39
39
40
``` rust
40
41
let s = " foo\
0 commit comments