Crate module/naming within an organization

In C++ there are some traditions around namespacing. Something I have seen at multiple organizations is name spacing along the lines of:

namespace company::project::folder1::folder2 { ... }

Company might keep everything in a monorepo of many binaries and libraries. Generally the namespacing mirrors the on disk folder structure. They try to keep the company name unique enough that it's unlikely to collide with anyone else they may merge with in the future. As long as they get that part right, they don't have to worry about collisions with the external world. They may give a project a super common name like "vector" but since it is inside the company namespace it is unlikely to ever actually conflict with another library. Even if they open source their vector library later, as long as they keep it in company::vector they are unlikely to need to change anything.

The global crate namespace is flat though. With rust even if I organize my monorepo a certain way, no matter where I put a crate in the folder structure it still goes into the same global namespace. So if I use a common name like "vector" I might need to rename it later if I open source my library, or if my company merges with another company that has a "vector" library.

Is there a better solution than naming every crate something hideous like "company_vector"? This could go on for multiple levels deep. Also, if you want to leave open the possibility of open sourcing your code eventually, should you be registering empty crates with crates.io to reserve names for those libraries?

You might be interested in the Packages as Optional Namespaces proposal, currently being prototyped with help from the crates.io team. In this proposal, Cargo package names can contain the / character, and the namespace company/* is reserved for the owner of the company package.

The proposal currently allows a single level of nesting (company/foo, but not company/foo/bar), though it reserves the possibility of multiple levels as a future extension.

For now, note that it is possible to publish a library named vector under a package name like company-vector. Also, downstream crates that uses multiple libraries named vector can rename one of both of them to avoid conflicts.

4 Likes