有没有办法让下面的代码工作?也就是说,在类型别名下导出枚举,并允许以新名称访问变体?
enum One { A, B, C }
type Two = One;
fn main() {
// error: no associated item named `B` found for type `One` in the current scope
let b = Two::B;
}
答案 0 :(得分:11)
我不认为类型别名允许执行您想要的操作,但您可以在use
语句中重命名枚举类型:
enum One { A, B, C }
fn main() {
use One as Two;
let b = Two::B;
}
您可以将此项与pub use
结合使用,以使用其他标识符重新导出类型:
mod foo {
pub enum One { A, B, C }
}
mod bar {
pub use foo::One as Two;
}
fn main() {
use bar::Two;
let b = Two::B;
}