From 53e43ae2eb8941e008a7657c9965ed63880b9d98 Mon Sep 17 00:00:00 2001 From: Jonathan Spira Date: Sat, 7 Jun 2025 16:39:53 -0400 Subject: [PATCH 1/2] &str and &[u8] have the same layout --- src/dynamically-sized-types.md | 5 +++-- src/type-layout.md | 1 + src/types/textual.md | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/dynamically-sized-types.md b/src/dynamically-sized-types.md index 28c398b7d..7434cbb77 100644 --- a/src/dynamically-sized-types.md +++ b/src/dynamically-sized-types.md @@ -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 DSTs. r[dynamic-sized.restriction] @@ -14,7 +14,7 @@ Such types can only be used in certain cases: r[dynamic-sized.pointer-types] * [Pointer types] to DSTs 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] @@ -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 diff --git a/src/type-layout.md b/src/type-layout.md index 5e202209d..2773a0864 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -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 diff --git a/src/types/textual.md b/src/types/textual.md index a6ad38ffc..32722e720 100644 --- a/src/types/textual.md +++ b/src/types/textual.md @@ -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 From 3da1948a9bf6a42672e13580cb4131b64f94229e Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Tue, 15 Jul 2025 22:50:49 +0000 Subject: [PATCH 2/2] Revise editorially --- src/dynamically-sized-types.md | 8 ++------ src/type-layout.md | 3 +-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/dynamically-sized-types.md b/src/dynamically-sized-types.md index 7434cbb77..ce3911b54 100644 --- a/src/dynamically-sized-types.md +++ b/src/dynamically-sized-types.md @@ -2,11 +2,7 @@ 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], [trait objects], and [str] are three examples of DSTs. +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 DSTs. r[dynamic-sized.restriction] Such types can only be used in certain cases: @@ -14,7 +10,7 @@ Such types can only be used in certain cases: r[dynamic-sized.pointer-types] * [Pointer types] to DSTs are sized but have twice the size of pointers to sized types - * Pointers to slices and `str` 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] diff --git a/src/type-layout.md b/src/type-layout.md index 2773a0864..425f82d5c 100644 --- a/src/type-layout.md +++ b/src/type-layout.md @@ -109,8 +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]`. -A reference `&str` has the same layout as a reference `&[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