Skip to content

Commit 2d9827f

Browse files
authored
Merge pull request #1848 from sanbox-irl/str-is-same-as-slice
&str and &[u8] have the same layout
2 parents d179e97 + 3da1948 commit 2d9827f

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

src/dynamically-sized-types.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@ r[dynamic-sized]
22
# Dynamically Sized Types
33

44
r[dynamic-sized.intro]
5-
Most types have a fixed size that is known at compile time and implement the
6-
trait [`Sized`][sized]. A type with a size that is known only at run-time is
7-
called a _dynamically sized type_ (_DST_) or, informally, an unsized type.
8-
[Slices] and [trait objects] are two examples of <abbr title="dynamically sized
9-
types">DSTs</abbr>.
5+
Most types have a fixed size that is known at compile time and implement the trait [`Sized`][sized]. A type with a size that is known only at run-time is called a _dynamically sized type_ (_DST_) or, informally, an unsized type. [Slices], [trait objects], and [str] are examples of <abbr title="dynamically sized types">DSTs</abbr>.
106

117
r[dynamic-sized.restriction]
128
Such types can only be used in certain cases:
139

1410
r[dynamic-sized.pointer-types]
1511
* [Pointer types] to <abbr title="dynamically sized types">DSTs</abbr> are
1612
sized but have twice the size of pointers to sized types
17-
* Pointers to slices also store the number of elements of the slice.
13+
* Pointers to slices and `str` also store the number of elements.
1814
* Pointers to trait objects also store a pointer to a vtable.
1915

2016
r[dynamic-sized.question-sized]
@@ -38,6 +34,7 @@ r[dynamic-sized.struct-field]
3834
3935
[sized]: special-types-and-traits.md#sized
4036
[Slices]: types/slice.md
37+
[str]: types/textual.md
4138
[trait objects]: types/trait-object.md
4239
[Pointer types]: types/pointer.md
4340
[Variables]: variables.md

src/type-layout.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Slices have the same layout as the section of the array they slice.
109109
r[layout.str]
110110
## `str` Layout
111111

112-
String slices are a UTF-8 representation of characters that have the same layout as slices of type `[u8]`.
112+
String slices are a UTF-8 representation of characters that have the same layout as slices of type `[u8]`. A reference `&str` has the same layout as a reference `&[u8]`.
113113

114114
r[layout.tuple]
115115
## Tuple Layout

src/types/textual.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ is valid UTF-8. Calling a `str` method with a non-UTF-8 buffer can cause
2323

2424
r[type.text.str-unsized]
2525
Since `str` is a [dynamically sized type], it can only be instantiated through a
26-
pointer type, such as `&str`.
26+
pointer type, such as `&str`. The layout of `&str` is the same as the layout of
27+
`&[u8]`.
2728

2829
r[type.text.layout]
2930
## Layout and bit validity

0 commit comments

Comments
 (0)