Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

类型别名

Syntax
TypeAlias
    type IDENTIFIER GenericParams? ( : TypeParamBounds )?
        WhereClause?
        ( = Type WhereClause? )? ;

类型别名 为其所在的模块或代码块的 类型命名空间 中的现有 类型 定义一个新名称。 类型别名使用关键字 type 声明。 每个值都有一个单一、具体的类型,但可能实现几个不同的 特型,并且可能与几种不同的类型约束兼容。

例如,下面将类型 Point 定义为类型 (u8, u8)(即 8 位无符号整数对的类型)的同义词:

#![allow(unused)]
fn main() {
type Point = (u8, u8);
let p: Point = (41, 68);
}

指向 元组结构体 或 单元结构体 的类型别名不能用于限定该类型的构造函数:

#![allow(unused)]
fn main() {
struct MyStruct(u32);

use MyStruct as UseAlias;
type TypeAlias = MyStruct;

let _ = UseAlias(5); // 正常
let _ = TypeAlias(5); // 无法工作
}

当类型别名不作为 关联类型 使用时,必须包含一个 类型 且不得包含 TypeParamBounds

当类型别名在 特型 中作为 关联类型 使用时,不得包含 类型 规范,但可以包含 TypeParamBounds

当类型别名在 特型实现 中作为 关联类型 使用时,必须包含一个 类型 规范,且不得包含 TypeParamBounds

特型实现 的类型别名中,等号之前的 Where 子句(如 type TypeAlias<T> where T: Foo = Bar<T>)已被弃用。等号之后的 Where 子句(如 type TypeAlias<T> = Bar<T> where T: Foo)是首选。