How do you import and reference enum types in Rust?

前端 未结 2 611
耶瑟儿~
耶瑟儿~ 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:44

    Editor's note: This answer predates Rust 1.0 and is not applicable for Rust 1.0.

    Enum variants are not scoped inside the enum; they are thus std::sync::atomics::Relaxed, &c.

    Scoped enum variants is the subject of issue 10090.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题