A type alias is not a new type¶
Level: 201 · working knowledge
One line: type Kilometers = u32; gives u32 a second name and nothing else — a Kilometers and a Miles add together without a word from the compiler — so an alias shortens a long type, and a newtype is what makes two types different.
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.
What it has to cover¶
- The same program with
type Km = u32; type Mi = u32;and withstruct Km(u32); struct Mi(u32);— the first adds them, the second refuses withE0308 - What aliases are for: shortening
Box<dyn Fn(u32) -> Result<(), MyError> + Send>and pinning a parameter, asio::Result<T>pinsE(see theResultalias) - Generic aliases:
type Grid<T> = Vec<Vec<T>>; - Bounds in an alias are not enforced — the
type_alias_boundslint, and what it says - An alias cannot carry an
implof its own:impl Km { … }is an inherent impl onu32, and rustc 1.98.0 refuses it withE0390cannot define inherentimplfor primitive types - Aliases in an API: rustdoc shows the alias, the compiler error shows the expanded type, and the reader has to connect them
The trap it exists for¶
Reaching for type UserId = u64; to stop IDs being mixed up. It documents the intent and enforces none of it — fn delete(user: UserId) accepts an order number without complaint. That job needs a tuple struct.
Where this sits¶
A score is not a number: the newtype is the version that creates a distinct type. The Result you are reading is probably an alias is the alias you meet most. Units are types is the newtype applied to measurements.
See also¶
- A score is not a number: the newtype — the distinct type an alias is not
- The
Resultyou are reading is probably an alias —io::Result<T>and friends - Units are types — kilometres and miles as real types
- What a struct is — the tuple struct a newtype is made of
- Wrapper types — when a newtype should implement
Deref - TYPES.md — where aliases sit among the ways to name a type
- Rust: The Practical Guide, chapter 2 exercises, run — exercise 2.8:
type Book = (String, String, u32);declared insidemain, andtype_nameprinting the tuple the alias stands for
If you are coming from another language¶
- C.
typedef unsigned int km;is exactly a Rust alias — same type, new spelling, no checking. - Go. Go has both, with a one-character difference:
type Km = uint32is an alias,type Km uint32is a new defined type that will not mix withMi. Rust spells the second as a tuple struct. - Python.
Km: TypeAlias = intis an alias for a checker;NewType("Km", int)is the newtype, checked only by the type checker and gone at run time. - C++.
using Km = unsigned;is an alias; a distinct type needs a wrapper struct, as in Rust.
Po polsku¶
Alias typu (type Km = u32;) to tylko druga nazwa tego samego typu — kompilator bez słowa doda kilometry do mil. Do skracania długich typów alias jest świetny; do rozróżniania wartości potrzebny jest newtype, czyli struktura krotkowa struct Km(u32);.
Szukaj po polsku: alias typu · newtype w Ruście · rust type alias vs newtype · rust type alias bounds not enforced