How do you import and reference enum types in Rust?

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

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.

Tags:

Rust