Skip to content

Commit 707517e

Browse files
committed
Rollup merge of rust-lang#32478 - xevix:docs/strings-str-unsized-types, r=steveklabnik
Add note on `str` being an unsized type in strings section of book The book section on Rust strings mentions `&str` and `String` but does not address why `str` is not used directly. This adds a short blurb and a link to the unsized types chapter. The second draft of the book will go more in-depth on this, but this should help a bit for now. Thanks #rust for clarifying this point, and let me know if it needs rewording or different placement 😄. CC @steveklabnik @Kimundi
2 parents 71fa130 + 6ce63fb commit 707517e

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/doc/book/strings.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ let s = "foo\
4444
assert_eq!("foobar", s);
4545
```
4646

47+
Note that you normally cannot access a `str` directly, but only through a `&str`
48+
reference. This is because `str` is an unsized type which requires additional
49+
runtime information to be usable. For more information see the chapter on
50+
[unsized types][ut].
51+
4752
Rust has more than only `&str`s though. A `String` is a heap-allocated string.
4853
This string is growable, and is also guaranteed to be UTF-8. `String`s are
4954
commonly created by converting from a string slice using the `to_string`
@@ -185,5 +190,6 @@ let hello_world = hello + &world;
185190
This is because `&String` can automatically coerce to a `&str`. This is a
186191
feature called ‘[`Deref` coercions][dc]’.
187192

193+
[ut]: unsized-types.html
188194
[dc]: deref-coercions.html
189195
[connect]: ../std/net/struct.TcpStream.html#method.connect

0 commit comments

Comments
 (0)