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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/dynamically-sized-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
[Slices], [trait objects], and [str] are three examples of <abbr title="dynamically sized
types">DSTs</abbr>.

r[dynamic-sized.restriction]
Expand All @@ -14,7 +14,7 @@ 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 of the slice.
* Pointers to trait objects also store a pointer to a vtable.

r[dynamic-sized.question-sized]
Expand All @@ -38,6 +38,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
1 change: 1 addition & 0 deletions src/type-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ r[layout.str]
## `str` Layout

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