Skip to content

Help new users understand the unintuitive crate name -> Rust namespace mapping for hyphenated names #15887

@Timmmm

Description

@Timmmm

Problem

An annoying papercut with Rust is crate names containing hyphens, e.g. aho-carosick.

It has been a number of years since I learned this, but at some point I knew that you accessed crate members based on the crate name:

use crate_name::foo;

but I did not know that crate names with hyphens were automatically converted to underscores. I know it sounds obvious if you already know it, but really - this is not obvious, nor is it easy to search for a solution.

Even worse if lib.name is set (see also #6827)

I think Cargo (and crates.io, but I guess that's out of scope here) could do more to help:

Proposed Solution

  1. When you create a new crate via cargo init with a crate name containing hyphens, warn that it may be confusing to new users because the crate name doesn't match the crate name. This could be a one-time or few-time warning, so as not to annoy people. See also Ask for confirmation of certain crate data on first publish #8883
  2. When you cargo add aho-carosick, note that you will need to use aho_carosick in code. Ditto on few-time warnings. Currently it actually warns in the opposite direction which is kind of silly: warning: translating regex_syntax to regex-syntax.
  3. crates.io should list the command as cargo add aho_carosick not cargo add aho-carosick. Both work but the former makes it more obvious how to use the code.

It's going to be really easy for seasoned Rust developers (including me, now) to dismiss this as trivial and obvious. Trust me it wasn't when I was learning, and I'm sure it isn't for many other people.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`Command-addS-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions