Skip to content

Commit 4cccd5b

Browse files
Glossary: add new entry documenting zero-sized types
1 parent 8c88f9d commit 4cccd5b

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/glossary.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,35 @@ 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+
- [Function items], or the constructors of a tuple-like structs or enum variants (see [type.fn-item.intro])
221+
- [Arrays] of zero-sized types (see [layout.array]).
222+
- [Arrays] of length zero (see [layout.array]).
223+
- [Unions] of zero-sized types (see [items.union.common-storage]).
224+
225+
```rust
226+
# use core::mem::{size_of, size_of_val};
227+
union U {
228+
f1: (),
229+
f2: [(); 10],
230+
f3: [u8; 0],
231+
}
232+
assert_eq!(0, size_of::<()>());
233+
assert_eq!(0, size_of_val(&main));
234+
assert_eq!(0, size_of::<[(); 10]>());
235+
assert_eq!(0, size_of::<[u8; 0]>());
236+
assert_eq!(0, size_of::<U>());
237+
```
238+
214239
[`extern` blocks]: items.extern
215240
[`extern fn`]: items.fn.extern
216241
[alignment]: type-layout.md#size-and-alignment
242+
[arrays]: type.array
217243
[associated item]: #associated-item
218244
[attributes]: attributes.md
219245
[*entity*]: names.md
@@ -222,6 +248,7 @@ A type is uninhabited if it has no constructors and therefore can never be insta
222248
[enums]: items/enumerations.md
223249
[fields]: expressions/field-expr.md
224250
[free item]: #free-item
251+
[function items]: type.fn-item
225252
[generic parameters]: items/generics.md
226253
[identifier]: identifiers.md
227254
[identifiers]: identifiers.md
@@ -252,5 +279,6 @@ A type is uninhabited if it has no constructors and therefore can never be insta
252279
[types]: types.md
253280
[undefined-behavior]: behavior-considered-undefined.md
254281
[unions]: items/unions.md
282+
[unit type]: type.tuple.unit
255283
[variable bindings]: patterns.md
256284
[visibility rules]: visibility-and-privacy.md

0 commit comments

Comments
 (0)