如何从Rust std lib导入和引用枚举类型?
我正在尝试使用Ordering
模块中的std::sync::atomics
枚举。
到目前为止,我的尝试都以失败告终:
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);
}
我目前正在使用Rust v.9。
答案 0 :(得分:8)
编者注:此答案早于Rust 1.0,不适用于Rust 1.0。
枚举变体不在enum中;因此,std::sync::atomics::Relaxed
,& c。
范围枚举变体是the subject of issue 10090。
答案 1 :(得分:8)
从Rust 1.0开始,枚举变体的范围在其枚举类型中。它们可以直接use
d:
pub use self::Foo::{A, B};
pub enum Foo {
A,
B,
}
fn main() {
let a = A;
}
或者您可以使用类型限定名称:
pub enum Foo {
A,
B,
}
fn main() {
let a = Foo::A;
}