Skip to content

Commit 1e87ac7

Browse files
Glossary: add new entry documenting zero-sized types
1 parent 3199c6a commit 1e87ac7

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

src/glossary.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,27 @@ r[glossary.uninhabited]
211211

212212
A type is uninhabited if it has no constructors and therefore can never be instantiated. An uninhabited type is "empty" in the sense that there are no values of the type. The canonical example of an uninhabited type is the [never type] `!`, or an enum with no variants `enum Never { }`. Opposite of [Inhabited](#inhabited).
213213

214+
r[glossary.zst]
215+
### Zero-sized type (ZST)
216+
217+
A type is zero-sized (a ZST) if its size is 0. Such types have only one possible value. Examples include:
218+
219+
- The [unit type].
220+
- [Unit-like structs] which have no fields.
221+
- [Unions] of zero-sized types.
222+
223+
```rust
224+
# use core::mem::{ManuallyDrop, size_of};
225+
struct S;
226+
union U {
227+
f1: (),
228+
f2: ManuallyDrop<S>,
229+
}
230+
assert_eq!(0, size_of::<()>());
231+
assert_eq!(0, size_of::<S>());
232+
assert_eq!(0, size_of::<U>());
233+
```
234+
214235
[`extern` blocks]: items.extern
215236
[`extern fn`]: items.fn.extern
216237
[alignment]: type-layout.md#size-and-alignment
@@ -252,5 +273,7 @@ A type is uninhabited if it has no constructors and therefore can never be insta
252273
[types]: types.md
253274
[undefined-behavior]: behavior-considered-undefined.md
254275
[unions]: items/unions.md
276+
[unit-like structs]: items.struct.unit
277+
[unit type]: type.tuple.unit
255278
[variable bindings]: patterns.md
256279
[visibility rules]: visibility-and-privacy.md

0 commit comments

Comments
 (0)