Skip to content

Commit c3c94d0

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

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/glossary.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,32 @@ 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+
- [Arrays] of length zero (see [layout.array]).
222+
- [Unions] of zero-sized types (see [items.union.common-storage]).
223+
224+
```rust
225+
# use core::mem::size_of;
226+
union U {
227+
f1: (),
228+
f2: [(); 10],
229+
}
230+
assert_eq!(0, size_of::<()>());
231+
assert_eq!(0, size_of::<[(); 10]>());
232+
assert_eq!(0, size_of::<[u8; 0]>());
233+
assert_eq!(0, size_of::<U>());
234+
```
235+
214236
[`extern` blocks]: items.extern
215237
[`extern fn`]: items.fn.extern
216238
[alignment]: type-layout.md#size-and-alignment
239+
[arrays]: type.array
217240
[associated item]: #associated-item
218241
[attributes]: attributes.md
219242
[*entity*]: names.md
@@ -252,5 +275,6 @@ A type is uninhabited if it has no constructors and therefore can never be insta
252275
[types]: types.md
253276
[undefined-behavior]: behavior-considered-undefined.md
254277
[unions]: items/unions.md
278+
[unit type]: type.tuple.unit
255279
[variable bindings]: patterns.md
256280
[visibility rules]: visibility-and-privacy.md

0 commit comments

Comments
 (0)