Rust type/values namespace status


#1

Rust currently uses separate namespaces for types and values which allows some funny patterns, like “real constructors”.

But looks like this feature is completely undocumented. So what’s the status of this? Is this a part of the language definition, or an implementation detail which can be changed in the future?

And are there any relevant docs besides source code of librustc_resovle?


#2

There are 4 namespaces actually! In addition to types and values, lifetimes have their own namespace. Loop labels technically use value namespace in the compiler, but they don’t intersect neither with values nor with lifetimes.

And there will be one more namespace soon, for macros. I’d say everything in this area is pretty stable and part of the language, except maybe the interaction between lifetimes and labels.[quote=“matklad, post:1, topic:3734”] And are there any relevant docs besides source code of librustc_resovle? [/quote]

Maybe https://github.com/nrc/rfcs/blob/6098278299d00d184d5b735ca6751c3e1765b406/text/0000-name-resolution.md or https://github.com/petrochenkov/rfcs/blob/433edd99f195d013c2d6e3a350fd2bd939cd88da/text/0000-adt-kinds.md (note that both are not accepted at the moment), maybe some old RFCs.


#3

So when I use foo::Bar; do I get the import for both namespaces? Is there any way to import just for one specific namespace?


#4

You get the import for both, and you can’t pick only one.


#5

In fact you can import something into only the type namespace using type Bar=foo::Bar;. This results in some interesting (read: confusing) behavior: https://github.com/rust-lang/rust/issues/26953


#6

If you’re interested in some of the nitty-gritty around namespaces, RFC 1560 specifies some of the interesting behviour (although it is not the main part of the RFC).

To answer the OP, it is a stable part of the language and will not change in a backwards incompatible way (though there may be backwards compatible changes, such as adding the macro namespace). I’m not aware of any documentation specifically on this topic (other than the above RFC).