Skip to content

Commit 1be8fcb

Browse files
committed
Make note of str in 'more strings' chapter
Fixes #21035
1 parent ecf8c64 commit 1be8fcb

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/doc/trpl/more-strings.md

+31-2
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)