编辑说明:从Rust 1.43版本开始,这个功能已经按预期工作。
我有一个类型别名type CardId = u64;
,我想通过std::u64::MAX
常量将它初始化为它所能拥有的最大值。但很惊讶地发现,我不能从别名直接实现。
use std::u64;
type CardId = u64;
fn main() {
let this_works = u64::MAX;
let this_doesnt_work = CardId::MAX;
println!("Max amount in integer: {} and {}", this_works, this_doesnt_work);
}
我原以为 MAX
常量也可以从类型别名中访问。当我将类型更改为 u32 时,这将有助于我,因为代码会有两个需要修改的点,而不仅仅是类型别名的位置。为什么要做出这个决定,我是否错过了某些可能使其成为可能的东西?