File tree Expand file tree Collapse file tree 1 file changed +31
-2
lines changed Expand file tree Collapse file tree 1 file changed +31
-2
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ Additionally, strings are not null-terminated and can contain null bytes.
1212
1313Rust has two main types of strings: ` &str ` and ` String ` .
1414
15- # &str
15+ # ` &str `
1616
1717The first kind is a ` &str ` . This is pronounced a 'string slice'.
1818String literals are of the type ` &str ` :
@@ -36,7 +36,36 @@ Like vector slices, string slices are simply a pointer plus a length. This
3636means that they're a 'view' into an already-allocated string, such as a
3737string 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
4170A ` String ` is a heap-allocated string. This string is growable, and is
4271also guaranteed to be UTF-8. ` String ` s are commonly created by
You can’t perform that action at this time.
0 commit comments