Zero sized types

Rust also supports zero sized types or ZSTs; structs with no fields, the unit type (), or arrays with no size are all zero sized. The fact that the type has no size is a boon to optimization and dead-tree removal. Rust APIs often include return types like so: Result<(), a_mod::ErrorKind>. This type signals that while the function may error, its return value in the happy path is the unit type. These types are somewhat rare in practice but unsafe, and Rust must be aware of them. Many allocators return null when asked to allocate zero bytes—making the allocation of a ZST indistinguishable from the allocator being unable to find free memory—and pointer offsets from a ZST are of zero offset. Both of these considerations will be ...

Get Hands-On Concurrency with Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.