Skip to content

Commit 2097a1e

Browse files
committed
---
yaml --- r: 192236 b: refs/heads/master c: 68af512 h: refs/heads/master v: v3
1 parent ce702c7 commit 2097a1e

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 1c25aecf675d731938951d048a45fb665105b441
2+
refs/heads/master: 68af51212350094cd1533a7e7f37106241628731
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a923278c6278c63468d74772c58dbf788e88f58c
55
refs/heads/try: ce76bff75603a754d092456285ff455eb871633d

trunk/src/doc/trpl/more-strings.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Additionally, strings are not null-terminated and can contain null bytes.
1212

1313
Rust has two main types of strings: `&str` and `String`.
1414

15-
# &str
15+
# `&str`
1616

1717
The first kind is a `&str`. This is pronounced a 'string slice'.
1818
String literals are of the type `&str`:
@@ -36,7 +36,36 @@ Like vector slices, string slices are simply a pointer plus a length. This
3636
means that they're a 'view' into an already-allocated string, such as a
3737
string literal or a `String`.
3838

39-
# String
39+
## `str`
40+
41+
You may occasionally see references to a `str` type, without the `&`. While
42+
this type does exist, it’s not something you want to use yourself. Sometimes,
43+
people confuse `str` for `String`, and write this:
44+
45+
```rust
46+
struct S {
47+
s: str,
48+
}
49+
```
50+
51+
This leads to ugly errors:
52+
53+
```text
54+
error: the trait `core::marker::Sized` is not implemented for the type `str` [E0277]
55+
note: `str` does not have a constant size known at compile-time
56+
```
57+
58+
Instead, this `struct` should be
59+
60+
```rust
61+
struct S {
62+
s: String,
63+
}
64+
```
65+
66+
So let’s talk about `String`s.
67+
68+
# `String`
4069

4170
A `String` is a heap-allocated string. This string is growable, and is
4271
also guaranteed to be UTF-8. `String`s are commonly created by

0 commit comments

Comments
 (0)