rust naming ai_generated true

can't find crate — Rust crate name has hyphens but use/extern expects underscores

ID: rust/hyphen-underscore-crate-name-confusion

Also available as: JSON · Markdown
92%Fix Rate
93%Confidence
4Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Cargo allows hyphens in crate names (my-crate) but Rust source code requires underscores (my_crate) for use/extern statements.

generic

Workarounds

  1. 98% success Use underscores in Rust source code: use my_crate (Cargo auto-maps hyphens to underscores)
    use my_crate::something;  // Cargo.toml has [dependencies] my-crate = "1.0" but Rust uses underscores

    Sources: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html

  2. 82% success If renaming is needed, use the [lib] name field in Cargo.toml to set a custom lib name
    [lib]
    name = "my_custom_name"  # explicit lib name separate from package name

    Sources: https://doc.rust-lang.org/cargo/reference/cargo-targets.html#the-name-field

Dead Ends

Common approaches that don't work:

  1. Use hyphens in use statement matching Cargo.toml 98% fail

    Rust identifiers cannot contain hyphens; the compiler parses my-crate as my minus crate

  2. Rename the Cargo.toml package name to remove hyphens 70% fail

    This changes the published crate name; breaks existing dependents and crates.io registry entry