Dylan
Dylan

Reputation: 1722

Glob imports of local enums in functions

The recent enum namespacing changes in Rust have broken some of my old code. In order to try and get it to compile again, I have tried adding glob imports. Unfortunately I can't get this to work for enums defined within a function. I can't find a way to import the variants of a local enum into the local namespace.

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

What is the appropriate import statement to use in this case?

Upvotes: 2

Views: 449

Answers (1)

DK.
DK.

Reputation: 59115

Unfortunately, I don't believe this to be possible.

use statements are absolute by default. As such, use Foo::*; won't work because Foo is not in the root module. use self::Foo::*; doesn't work because self refers to the containing module, not the containing scope (which in this case is a function within the containing module).

You can kind of work around this by placing the function and the enum in a module of their own, then re-export the function to the containing module.

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

Upvotes: 2

Related Questions