`use x as y` but then no `use y::z::*`

Dear Rustaceans,

Consider this code:

pub mod elcapitan;
pub mod yosemite;
// Conditional stuff
use elcapitan as mac;

One can for the most part use mac as one would use elcapitan; however, using from within mac does not seem to work as I would hope:

use mac::Enum_NSButtons::*;

The error given is something like:

error: unresolved import `mac::Enum_NSButtons::*`. Maybe a missing `extern crate mac`? [E0432]

Now it might be the case that I am not using the module system right; but it seems like something that should work.

-- Bitter About Modules

pub use elcapitan as mac;?

Dear Rustaceans,

Wow. Sweet.

-- Delighted With Modules

The way I conceptualize this, which may be somewhat off the mark (the name lookup rules are not clearly stated in the documentation), is that there are two separate namespaces here. There is a module namespace and a lexical namespace. Ordinary use looks up things in the module namespace and installs them in the lexical namespace; as such, ordinary uses cannot see each other. pub use also writes into the current module, so a pub use can be seen by future uses.

This is also why the behavior of relative and absolute paths in use statements (use std::foo, use self::bar) and expressions (::std::foo, bar) differs; there are different rules for starting in the module tree versus lexical scope. extern crate is a bit odd because it writes into lexical scope and exposes a module at the same time.

1 Like

I like to think that paths in use items are implicitly global.[quote="sorear, post:4, topic:4744"]
Ordinary use looks up things in the module namespace and installs them in the lexical namespace; as such, ordinary uses cannot see each other. pub use also writes into the current module, so a pub use can be seen by future uses.
[/quote]

Thanks! I was thinking a little different and was mistaking.