简体   繁体   English

如何在Rust中导入和引用枚举类型?

[英]How do you import and reference enum types in Rust?

How do you import and reference enum types from the Rust std lib? 如何从Rust std lib导入和引用枚举类型?

I'm trying to use the Ordering enum from the std::sync::atomics module. 我正在尝试使用std::sync::atomics模块中的Ordering枚举。 My attempts so far have all ended in failure: 到目前为止,我的尝试都以失败告终:

use std::sync::atomics::AtomicBool;
use std::sync::atomics::Ordering;

// error unresolved import: there is no `Relaxed` in `std::sync::atomics::Ordering`
// use std::sync::atomics::Ordering::Relaxed;  

fn main() {
    let mut ab = AtomicBool::new(false);
    let val1 = ab.load(Ordering::Relaxed); // error: unresolved import:
                                           // there is no `Relaxed` in `std::sync::atomics::Ordering`
    println!("{:?}", val1);

    ab.store(true, Ordering.Relaxed);      // error: unresolved name `Ordering`
    let val2 = ab.load(Ordering(Relaxed)); // error: unresolved name `Relaxed`
    println!("{:?}", val2);    
}

I'm currently using Rust v. 0.9. 我目前正在使用Rust v。0.9。

As of Rust 1.0, enum variants are scoped inside of their enum type. 从Rust 1.0开始,枚举变量的范围仅限于其枚举类型内部。 They can be directly use d: 它们可以直接use d:

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. 编者注:该答案早于Rust 1.0,不适用于Rust 1.0。

Enum variants are not scoped inside the enum; 枚举变量不在枚举内。 they are thus std::sync::atomics::Relaxed , &c. 因此它们是std::sync::atomics::Relaxed等。

Scoped enum variants is the subject of issue 10090 . 范围枚举变量是问题10090

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM