别名

可以用 type 语句给已有的类型取个新的名字。类型的名字必须遵循驼峰命名法(像是 CamelCase 这样),否则编译器将给出错误。原生类型是例外,比如: usize、f32,等等。

1. // `NanoSecond` 是 `u64` 的新名字。
2. type NanoSecond = u64;
3. type Inch = u64;

5. // 通过这个属性屏蔽警告。
6. #[allow(non_camel_case_types)]
7. type u64_t = u64;
8. // 试一试 ^ 移除上面那个属性

10. fn main() {
11. // `NanoSecond` = `Inch` = `u64_t` = `u64`.
12. let nanoseconds: NanoSecond = 5 as u64_t;
13. let inches: Inch = 2 as u64_t;

15. // 注意类型别名*并不能*提供额外的类型安全,因为别名*并不是*新的类型。
16. println!("{} nanoseconds + {} inches = {} unit?",
17. nanoseconds,
18. inches,
19. nanoseconds + inches);
20. }

别名的主要用途是避免写出冗长的模板化代码(boilerplate code)。如 IoResult<T> 是 Result<T, IoError> 类型的别名。

See also:

Attributes