可以使用type关键字声明另一类型的别名。
比如:
type unsigned_64 = u64;
然后就可以像使用u64
一样使用unsigned_64
// 允许以非驼峰的方式起别名,否则起unsigned_64别名时会提示: Type `unsigned_64` should have a camel case name such as `Unsigned64`
#![allow(non_camel_case_types)]
//为u64起别名
type unsigned_64 = u64;
#[test]
fn test_type() {
let i :unsigned_64 = 1;
println!("i={}" , i);
}
还可以使用泛型类型别名:
use std::result;
enum ConcreteError {
Foo,
Bar,
}
type Result<T> = result::Result<T, ConcreteError>;
给result::Result<T, ConcreteError>
这种类型起了别名Result<T>
,Result<T>
代表泛型类型result::Result<T, E>
的E
是ConcreteError
这种特定的情况。