Skip to content

Commit 7811820

Browse files
committed
&str and &[u8] have the same layout
1 parent 9ebf0f5 commit 7811820

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

src/dynamically-sized-types.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ r[dynamic-sized.intro]
55
Most types have a fixed size that is known at compile time and implement the
66
trait [`Sized`][sized]. A type with a size that is known only at run-time is
77
called a _dynamically sized type_ (_DST_) or, informally, an unsized type.
8-
[Slices] and [trait objects] are two examples of <abbr title="dynamically sized
8+
[Slices], [trait objects], and [str] are three examples of <abbr title="dynamically sized
99
types">DSTs</abbr>.
1010

1111
r[dynamic-sized.restriction]
@@ -14,7 +14,7 @@ Such types can only be used in certain cases:
1414
r[dynamic-sized.pointer-types]
1515
* [Pointer types] to <abbr title="dynamically sized types">DSTs</abbr> are
1616
sized but have twice the size of pointers to sized types
17-
* Pointers to slices also store the number of elements of the slice.
17+
* Pointers to slices and `str` also store the number of elements of the slice.
1818
* Pointers to trait objects also store a pointer to a vtable.
1919

2020
r[dynamic-sized.question-sized]
@@ -38,6 +38,7 @@ r[dynamic-sized.struct-field]
3838
3939
[sized]: special-types-and-traits.md#sized
4040
[Slices]: types/slice.md
41+
[str]: types/textual.md
4142
[trait objects]: types/trait-object.md
4243
[Pointer types]: types/pointer.md
4344
[Variables]: variables.md

src/type-layout.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Pointers to unsized types are sized. The size and alignment is guaranteed to be
8989
at least equal to the size and alignment of a pointer.
9090

9191
> [!NOTE]
92-
> Though you should not rely on this, all pointers to <abbr title="Dynamically Sized Types">DSTs</abbr> are currently twice the size of the size of `usize` and have the same alignment.
92+
> Though you should not rely on this, all pointers to <abbr title="Dynamically Sized Types">DSTs</abbr> have the same alignment as usize.
9393
9494
r[layout.array]
9595
## Array Layout
@@ -110,6 +110,7 @@ r[layout.str]
110110
## `str` Layout
111111

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

114115
r[layout.tuple]
115116
## 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)