为枚举键入别名

时间:2015-11-08 23:12:29

标签: rust

有没有办法让下面的代码工作?也就是说,在类型别名下导出枚举,并允许以新名称访问变体?

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;
}

1 个答案:

答案 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;
}
相关问题