← All notes

Rust advanced types: newtypes, type aliases, and the never type

Answer: The Rust type system has some features mentioned earlier but not yet discussed: newtypes, type aliases, the ! type, and dynamically sized types.

Newtypes and type aliases

The discussion starts with newtypes and why they are useful as types, then turns to type aliases, which are similar but have slightly different semantics.

Beyond earlier examples, the newtype pattern can statically keep values from being mixed up and can mark the units of a value.

Listing 20-16 wrapped u32 values in Millimeters and Meters for that reason.

The pattern is a lightweight way to hide implementation details, as discussed with encapsulation in Chapter 18.

A type alias, declared with the type keyword, gives an existing type another name.

Aliases do not add the type-checking benefits of a newtype: mixing Kilometers and i32 will not produce a compiler error.

The main use case for type synonyms is to reduce repetition. Type aliases are also commonly used with the Result<T, E> type for reducing repetition.

The never type

Rust also has a special type named !.

In type theory this is the empty type, because it has no values.

The book prefers the name never type, because it is used as the return type of a function that will never return.

The chapter also discusses dynamically sized types after this section.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Advanced Types - The Rust Programming Language; The Rust Project Developers, Advanced Types (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Advanced Types - The Rust Programming Language; The Rust Project Developers, Advanced Types (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers.

Cite this

Permanent ID: iil:note:3e6fxvnz

iinventedlight. 2026. "Rust advanced types: newtypes, type aliases, and the never type." iinventedlight Library, iil:note:3e6fxvnz, version of 2026-11-26. https://iinventedlight.com/library/notes/rust-advanced-types/. License: CC-BY-4.0.

@misc{iil_note_3e6fxvnz,
  author = {{iinventedlight}},
  title = {{Rust advanced types: newtypes, type aliases, and the never type}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-26},
  url = {https://iinventedlight.com/library/notes/rust-advanced-types/},
  note = {Permanent ID iil:note:3e6fxvnz. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-advanced-types.md.