Listing Error: Unresolved Name

Enum constructors defined in the same file are no longer allowed.

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

In which_mode function which_mode compiler generates the error message "Unresolved Global name". When I qualify it as Mode::Global , it works. Now he believes that Global in the match expression is binding and therefore irrefutable!

Last behavior - November 11th November successfully compiled the above code. Since this current behavior is like this, why Some , Ok , etc. Do not need qualified ways?

+5
source share
1 answer

As you noticed, most recently , changes are listed to have options with a name type .

The standard library has explicit re-exports of options , so they are available next to the type (for example, for the exact example linked there, core::option::None is an alias for core::option::Option::None ), so they are available in its module unskilled.

However, there is a trick: None , Some , Err , Ok are available only by default, because they are in the prelude, which by default is imported into each module. That is, namespace changes have not changed, why these options do not need to be qualified in most Rust code.

+4
source

Source: https://habr.com/ru/post/1207301/


All Articles