为枚举键入别名

Tim*_*son 12 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;
}
Run Code Online (Sandbox Code Playgroud)

fjh*_*fjh 11

我不认为类型别名允许执行您想要的操作,但您可以在use语句中重命名枚举类型:

enum One { A, B, C }

fn main() {
    use One as Two;
    let b = Two::B;
}
Run Code Online (Sandbox Code Playgroud)

您可以结合使用它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;
}
Run Code Online (Sandbox Code Playgroud)