E0432 rust module_error ai_generated true

error[E0432]: unresolved import `my-crate` -- could not find `my-crate` in the crate root

ID: rust/e0432-unresolved-import-crate-name-hyphen

Also available as: JSON · Markdown · 中文
98%Fix Rate
90%Confidence
1Evidence
2023-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
rustc 1.56.0 active
rustc 1.63.0 active
rustc 1.70.0 active

Root Cause

Rust crate names with hyphens are automatically converted to underscores in code; use `my_crate` instead of `my-crate` in use statements.

generic

中文

Rust 中带连字符的 crate 名称在代码中会自动转换为下划线;在 use 语句中应使用 `my_crate` 而不是 `my-crate`。

Official Documentation

https://doc.rust-lang.org/error_codes/E0432.html

Workarounds

  1. 98% success Replace hyphens with underscores in all use statements: `use my_crate::SomeType;`
    Replace hyphens with underscores in all use statements: `use my_crate::SomeType;`
  2. 95% success In Cargo.toml, use the `package` key to rename: `my_crate = { package = "my-crate", version = "1.0" }` then import as `my_crate`.
    In Cargo.toml, use the `package` key to rename: `my_crate = { package = "my-crate", version = "1.0" }` then import as `my_crate`.
  3. 85% success Use `extern crate my_crate as my_crate;` if needed for legacy code.
    Use `extern crate my_crate as my_crate;` if needed for legacy code.

中文步骤

  1. 在所有 use 语句中将连字符替换为下划线:`use my_crate::SomeType;`
  2. 在 Cargo.toml 中使用 `package` 键重命名:`my_crate = { package = "my-crate", version = "1.0" }`,然后以 `my_crate` 导入。
  3. 如果需要,使用 `extern crate my_crate as my_crate;` 用于遗留代码。

Dead Ends

Common approaches that don't work:

  1. 95% fail

    extern crate also uses the underscore convention. The hyphen is only valid in Cargo.toml dependency names, not in code.

  2. 70% fail

    Cargo.toml dependency names can have hyphens, but the crate name in Rust source is always underscores. Renaming in Cargo.toml is unnecessary.

  3. 95% fail

    Hyphens are not valid in Rust identifiers. Use underscores instead.