2015-11-08 187 views
11

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

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

回答

10

我不认为类型别名允许做你想要什么,而是你可以重命名的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; 
} 
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; 
} 
+0

重新导出没有窍门 - 我猜测枚举类型的行为更像微型模块而不是结构和基元。 –