Skip to content

Commit 0fab2b9

Browse files
committed
Rollup merge of rust-lang#22556 - brson:str, r=steveklabnik
Clarify that `to_string` is how you make `String`. Use a coercion in an example. r? @steveklabnik
2 parents 5972695 + 96be553 commit 0fab2b9

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/doc/trpl/crates-and-modules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Rust has two distinct terms that relate to the module system: *crate* and
1212
*module*. A crate is synonymous with a *library* or *package* in other
1313
languages. Hence "Cargo" as the name of Rust's package management tool: you
1414
ship your crates to others with Cargo. Crates can produce an executable or a
15-
shared library, depending on the project.
15+
library, depending on the project.
1616

1717
Each crate has an implicit *root module* that contains the code for that crate.
1818
You can then define a tree of sub-modules under that root module. Modules allow

src/doc/trpl/more-strings.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ string literal or a `String`.
3838

3939
# String
4040

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

4445
```
4546
let mut s = "Hello".to_string();
@@ -49,7 +50,7 @@ s.push_str(", world.");
4950
println!("{}", s);
5051
```
5152

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

5455
```
5556
fn takes_slice(slice: &str) {
@@ -58,7 +59,7 @@ fn takes_slice(slice: &str) {
5859
5960
fn main() {
6061
let s = "Hello".to_string();
61-
takes_slice(&*s);
62+
takes_slice(&s);
6263
}
6364
```
6465

src/doc/trpl/strings.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ compiled program, and exists for the entire duration it runs. The `string`
2525
binding is a reference to this statically allocated string. String slices
2626
have a fixed size, and cannot be mutated.
2727

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

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

0 commit comments

Comments
 (0)