Blog Post: No Namespaces in Rust is a Feature

A less obvious definition is that adding new dependencies shouldn’t stop you from compiling already working code. This is a major motivation for the orphan rule (though the orphan rule is more nuanced than that). This is a strike against schemes that encourage multiple distinct crates to have the same default name in code.

Could you expand on this? To me, unless I am missing something (which I may very well be), this "preference" is subjective.

Speaking for myself, for instance, I wouldn't mind encountering the following situation:

  1. Doing:

    cargo add http@namespace_name
    
    • (Invented namespace syntax, definitely not the point I wanna talk about)

    • Or manually editing the Cargo.toml file
      [dependencies]
      http = { package = "http@namespace_name", version = "..." }
      
  2. To then immediately get a cargo error complaining that http is an already present direct dependency, thus asking me to rename it to something else.

That is, I don't see that workflow as contradictory / surprising / undesirable


In other words / if other people feel the same, "namespaces" could be implemented as an auto-renaming sugar, especially provided something like cargo-edit's cargo {add,rm} were to be blessed into bundled-by-default-with-cargo, like cargo vendor or other subcommands did:

  1. Allow publishing crates to crates.io with some new optional syntax, such as a delimiting @, /, or :: sigil. For the sake of the example, I'll pick <name>@<namespace_name> syntax.

    • If we need retrocompat, then let's pick something like <namespace_name>--<name>
  2. Then, cargo add <name>@<namespace_name> would be equivalent to doing cargo add <name>@<namespace_name> --rename <name>, that is, in Cargo.toml syntax:

    [dependencies]
    <name> = { package = "<namespace_name>", version = "..." }
    
  • This would solve the "get a conflict when adding a dependency issue", since it would be no different of someone having already some dependency foo renamed as bar, and then trying to add the bar crate.

  • It would also provide the ergonomics (at least, as long as people use cargo add and the like), to avoid the current status quo whereby people manually prefix their crates and users then need to use long crate names or manually rename as they see fit.

2 Likes