Skip to content

Commit 2fb38fc

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

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

src/glossary.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,30 @@ 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 at most one possible value. Examples include:
218+
219+
- The [unit type] (see [layout.tuple.unit]).
220+
- [Arrays] of zero-sized types (see [layout.array]).
221+
- [Unions] of zero-sized types (see [items.union.common-storage]).
222+
223+
```rust
224+
# use core::mem::size_of;
225+
union U {
226+
f1: (),
227+
f2: [(); 10],
228+
}
229+
assert_eq!(0, size_of::<()>());
230+
assert_eq!(0, size_of::<[(); 10]>());
231+
assert_eq!(0, size_of::<U>());
232+
```
233+
214234
[`extern` blocks]: items.extern
215235
[`extern fn`]: items.fn.extern
216236
[alignment]: type-layout.md#size-and-alignment
237+
[arrays]: type.array
217238
[associated item]: #associated-item
218239
[attributes]: attributes.md
219240
[*entity*]: names.md
@@ -252,5 +273,6 @@ 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 type]: type.tuple.unit
255277
[variable bindings]: patterns.md
256278
[visibility rules]: visibility-and-privacy.md

0 commit comments

Comments
 (0)