Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enum error: unresolved name

Tags:

enums

rust

Enum constructors defined in the same file no longer resolve.

enum Mode {
    Global,
    Local,
}

fn which_mode() -> Mode {
    Global
}

fn main() {
    match which_mode() {
        Global => println!("Global"),
        Local  => println!("Local"),
    }
}

The compiler gives an error "unresolved name Global" in the function which_mode. When I qualify it as Mode::Global, it works. Now, it thinks that the Global in the match statement is a binding, and is hence irrefutable!

This behaviour is recent -- the nightly of 11 November successfully compiled the above code. With this current behaviour being how it is, why do Some, Ok, etc. not need qualified paths?

like image 903
JONNALAGADDA Srinivas Avatar asked Sep 30 '22 09:09

JONNALAGADDA Srinivas


1 Answers

As you noticed, very recently, enums changed to have the variants scoped with their type name.

The standard library has explicit reexports of the variants so they are available adjacent to the type (e.g. for the precise example linked there core::option::None is an alias for core::option::Option::None), which is why they're available unqualified in their module.

However, there's a trick here: None, Some, Err, Ok are only available by default because they are in the prelude, which is imported by default into every module. That is, the namespacing changes didn't change why those variants do not need qualification in most Rust code.

like image 134
huon Avatar answered Oct 11 '22 22:10

huon