Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 函数中本地枚举的全局导入_Rust - Fatal编程技术网

Rust 函数中本地枚举的全局导入

Rust 函数中本地枚举的全局导入,rust,Rust,最近Rust中的枚举名称空间更改破坏了我的一些旧代码。为了尝试重新编译,我尝试添加glob导入。不幸的是,对于函数中定义的枚举,我无法实现这一点。我找不到将本地枚举的变体导入本地命名空间的方法 #![feature(globs)] fn main() { use self::Foo::*; // Does not work enum Foo { Bar, Baz } let x = Bar; // Error - Bar not f

最近Rust中的枚举名称空间更改破坏了我的一些旧代码。为了尝试重新编译,我尝试添加glob导入。不幸的是,对于函数中定义的枚举,我无法实现这一点。我找不到将本地枚举的变体导入本地命名空间的方法

#![feature(globs)]
fn main() {
    use self::Foo::*; // Does not work
    enum Foo {
        Bar,
        Baz
    }
    let x = Bar; // Error - Bar not found
}

在这种情况下,应该使用什么样的导入语句?

不幸的是,我认为这是不可能的

use
语句默认为绝对语句。因此,
使用Foo::*不起作用,因为
Foo
不在根模块中<代码>使用self::Foo::*不起作用,因为
self
引用的是包含模块,而不是包含范围(在本例中是包含模块内的函数)

您可以通过将函数和枚举放在它们自己的模块中,然后将函数重新导出到包含模块来解决这个问题

use self::a::blah;
pub mod a {
    use self::Foo::*;
    enum Foo { Bar, Baz }
    fn blah() { /* use Bar and Baz... */ }
}