How do you import and reference enum types in Rust?

前端 未结 2 628
耶瑟儿~
耶瑟儿~ 2020-12-19 02:30

How do you import and reference enum types from the Rust std lib?

I\'m trying to use the Ordering enum from the std::sync::atomics module.

2条回答
  •  余生分开走
    2020-12-19 02:46

    As of Rust 1.0, enum variants are scoped inside of their enum type. They can be directly used:

    pub use self::Foo::{A, B};
    
    pub enum Foo {
        A,
        B,
    }
    
    fn main() {
        let a = A;
    }
    

    or you can use the type-qualified name:

    pub enum Foo {
        A,
        B,
    }
    
    fn main() {
        let a = Foo::A;
    }
    

提交回复
热议问题