Rust之type

可以使用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>EConcreteError这种特定的情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值