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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With