Skip to content

Commit

Permalink
book: Minor clarifications about strings
Browse files Browse the repository at this point in the history
  • Loading branch information
brson committed Feb 20, 2015
1 parent 522d09d commit 96be553
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/doc/trpl/crates-and-modules.md
Expand Up @@ -12,7 +12,7 @@ Rust has two distinct terms that relate to the module system: *crate* and
*module*. A crate is synonymous with a *library* or *package* in other
languages. Hence "Cargo" as the name of Rust's package management tool: you
ship your crates to others with Cargo. Crates can produce an executable or a
shared library, depending on the project.
library, depending on the project.

Each crate has an implicit *root module* that contains the code for that crate.
You can then define a tree of sub-modules under that root module. Modules allow
Expand Down
9 changes: 5 additions & 4 deletions src/doc/trpl/more-strings.md
Expand Up @@ -38,8 +38,9 @@ string literal or a `String`.

# String

A `String` is a heap-allocated string. This string is growable, and is also
guaranteed to be UTF-8.
A `String` is a heap-allocated string. This string is growable, and is
also guaranteed to be UTF-8. `String`s are commonly created by
converting from a string slice using the `to_string` method.

```
let mut s = "Hello".to_string();
Expand All @@ -49,7 +50,7 @@ s.push_str(", world.");
println!("{}", s);
```

You can coerce a `String` into a `&str` by dereferencing it:
A reference to a `String` will automatically coerce to a string slice:

```
fn takes_slice(slice: &str) {
Expand All @@ -58,7 +59,7 @@ fn takes_slice(slice: &str) {
fn main() {
let s = "Hello".to_string();
takes_slice(&*s);
takes_slice(&s);
}
```

Expand Down
6 changes: 4 additions & 2 deletions src/doc/trpl/strings.md
Expand Up @@ -25,8 +25,10 @@ compiled program, and exists for the entire duration it runs. The `string`
binding is a reference to this statically allocated string. String slices
have a fixed size, and cannot be mutated.

A `String`, on the other hand, is an in-memory string. This string is
growable, and is also guaranteed to be UTF-8.
A `String`, on the other hand, is a heap-allocated string. This string
is growable, and is also guaranteed to be UTF-8. `String`s are
commonly created by converting from a string slice using the
`to_string` method.

```{rust}
let mut s = "Hello".to_string(); // mut s: String
Expand Down

0 comments on commit 96be553

Please sign in to comment.