Skip to content

Update 'Strings' chapter of the book #29846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/doc/trpl/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ 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`. Let’s 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 it’s 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`. A string
literal is a string slice that is statically allocated, meaning that it’s saved
inside our compiled program, and exists for the entire duration it runs. The
`greeting` binding is a reference to this statically allocated string. Any
function expecting a string slice will also accept a string literal.

String literals can span multiple lines. There are two forms. The first will
include the newline and the leading spaces:
Expand All @@ -34,7 +35,7 @@ let s = "foo
assert_eq!("foo\n bar", s);
```

The second, with a `\`, does not trim the spaces:
The second, with a `\`, trims the spaces and the newline:

```rust
let s = "foo\
Expand Down