Update 'Strings' chapter of the book

Fix #29823 by further explaining `&str` and pointing out the
difference between `&str` and `&'static str`.
This commit is contained in:
Jan Likar 2015-11-15 06:55:30 +01:00
parent 4f5edf9e38
commit 2b05fdb38a

View File

@ -12,17 +12,17 @@ encoding of UTF-8 sequences. Additionally, unlike some systems languages,
strings are not null-terminated and can contain null bytes.
Rust has two main types of strings: `&str` and `String`. Lets talk about
`&str` first. These are called string slices. String literals are of the type
`&'static str`:
`&str` first. These are called string slices. A string slice has a fixed
size, and cannot be mutated. It is a reference to a sequence of UTF-8 bytes.
```rust
let greeting = "Hello there."; // greeting: &'static str
```
This string is statically allocated, meaning that its saved inside our
compiled program, and exists for the entire duration it runs. The `greeting`
binding is a reference to this statically allocated string. String slices
have a fixed size, and cannot be mutated.
`"Hello there."` is a string literal and its type is `&'static str`. String
literal is a string slice that is statically allocated, meaning that its saved
inside our compiled program, and exists for the entire duration it runs. The
`greeting` binding is a reference to this statically allocated string.
String literals can span multiple lines. There are two forms. The first will
include the newline and the leading spaces: