Skip to content

&str and &[u8] have the same layout #1848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/dynamically-sized-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ r[dynamic-sized]
# Dynamically Sized Types

r[dynamic-sized.intro]
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] and [trait objects] are two examples of <abbr title="dynamically sized
types">DSTs</abbr>.
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>.

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

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

r[dynamic-sized.question-sized]
Expand All @@ -38,6 +34,7 @@ r[dynamic-sized.struct-field]

[sized]: special-types-and-traits.md#sized
[Slices]: types/slice.md
[str]: types/textual.md
[trait objects]: types/trait-object.md
[Pointer types]: types/pointer.md
[Variables]: variables.md
Expand Down
2 changes: 1 addition & 1 deletion src/type-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Slices have the same layout as the section of the array they slice.
r[layout.str]
## `str` Layout

String slices are a UTF-8 representation of characters that have the same layout as slices of type `[u8]`.
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]`.

r[layout.tuple]
## Tuple Layout
Expand Down
3 changes: 2 additions & 1 deletion src/types/textual.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ is valid UTF-8. Calling a `str` method with a non-UTF-8 buffer can cause

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

r[type.text.layout]
## Layout and bit validity
Expand Down