The following code works as expected:
#[derive(Debug)]
enum Enum { A, B }
fn main() {
use Enum::*;
println!("{:?} {:?}", A, B);
}
However, the following, with the enum moved into the function, fails:
fn main() {
#[derive(Debug)]
enum Enum { A, B }
use Enum::*;
println!("{:?} {:?}", A, B);
}
The error message:
error: unresolved import `Enum::*`. Maybe a missing `extern crate Enum`? [--explain E0432]
I didn’t find any variation on self::
or super::
that makes that version work.
This makes it a bit more painful to use enums declared within a function. Some discussion on IRC suggested that function-local scopes don’t have a name, and use
can’t refer to them or use anything from them.
I’d like to propose an RFC allowing the example above to work as written.
This doesn’t seem likely to break any existing code, unless that code either 1) declares a function-local enum with the same name as a module with a non-snake-case name (which would produce a warning by default) and tries to use the module from within the same function, or 2) declares enums with identical names at top-level and within a function and tries to use the top-level enum from within the same function. Both of those seem highly unlikely.
Another alternative would involve introducing a name for the function-local scope, such as use fn::E
or use local::E
. However, that would then raise questions about syntax for nested functions, or nested blocks, and fn::super::super::
seems excessive. I’d prefer to just have use Name
within a function search upward through function scopes until it hits module scope.
Before writing this up as an RFC, I wanted to seek feedback here on the issue and this approach to solving it.