Rust尝试将外部crate导入到不在根目录中的模块时出现“unresolved import”。

huangapple go评论97阅读模式
英文:

Rust `unresolved import` when trying to import an external crate into a module not in the root directory

问题

I'm very new to Rust, so maybe this is an obvious question, but I couldn't find anything that answered it. I can't seem to import the external crate diesel_derive_enum in my module sub-module database/models.rs. It works perfectly if I simply move the models.rs file to the src/ directory. I'm guessing I'm missing something where I need to tell it to look up to the root level for the external crate?

I have a project that's structured like:

  1. src
  2. ├── database
  3. ├── models.rs
  4. ├── mod.rs
  5. └── schema.rs
  6. └── main.rs

Models.rs

  1. use diesel::pg::PgConnection;
  2. use diesel::prelude::*;
  3. use diesel_derive_enum;
  4. use uuid::Uuid;
  5. use super::schema::market_trade_goods;
  6. #[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)] // <- error is here
  7. #[ExistingTypePath = "crate::schema::sql_types::SupplyType"]
  8. pub enum SupplyType {
  9. Scarce,
  10. Limited,
  11. Moderate,
  12. Abundant,
  13. }
  14. // ...then more

database/mod.rs

  1. pub mod models;
  2. pub mod schema;

main.rs

  1. mod database;
  2. use diesel::pg::PgConnection;
  3. use diesel::prelude::*;
  4. use dotenvy::dotenv;
  5. use std::env;
  6. use uuid::Uuid;
  7. use crate::database::models::{SupplyType, TradeGood};
  8. // ...then some more functions and whatnot

I've tried a bunch of different ways of importing things like crate::diesel_derive_enum or extern crate diesel_derive_enum (in models.rs and main.rs). Nothing seems to make a difference, but I'm sure I'm just missing something obvious.

Edit: Full error message is:

  1. error[E0433]: failed to resolve: unresolved import
  2. --> src/database/models.rs:8:35
  3. |
  4. 8 | #[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)]
  5. | ^^^^^^^^^^^^^^^^^^^^^^^^^^
  6. | |
  7. | unresolved import
  8. | help: a similar path exists: `crate::database::schema`
  9. |
  10. = note: this error originates in the derive macro `diesel_derive_enum::DbEnum` (in Nightly builds, run with -Z macro-backtrace for more info)
英文:

I'm very new to Rust, so maybe this is an obvious question, but I couldn't find anything that answered it. I can't seem to import the external crate diesel_derive_enum in my module sub-module database/models.rs. It works perfectly if I simply move the models.rs file to the src/ directory. I'm guessing I'm missing something where I need to tell it to look up to the root level for the external crate?

I have a project that's structured like:

  1. src
  2. ├── database
  3. ├── models.rs
  4. ├── mod.rs
  5. └── schema.rs
  6. └── main.rs

Models.rs

  1. use diesel::pg::PgConnection;
  2. use diesel::prelude::*;
  3. use diesel_derive_enum;
  4. use uuid::Uuid;
  5. use super::schema::market_trade_goods;
  6. #[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)] // <- error is here
  7. #[ExistingTypePath = "crate::schema::sql_types::SupplyType"]
  8. pub enum SupplyType {
  9. Scarce,
  10. Limited,
  11. Moderate,
  12. Abundant,
  13. }
  14. // ...then more

database/mod.rs

  1. pub mod models;
  2. pub mod schema;

main.rs

  1. mod database;
  2. use diesel::pg::PgConnection;
  3. use diesel::prelude::*;
  4. use dotenvy::dotenv;
  5. use std::env;
  6. use uuid::Uuid;
  7. use crate::database::models::{SupplyType, TradeGood};
  8. // ...then some more functions and whatnot

I've tried a bunch of different ways of importing things like crate::diesel_derive_enum or extern crate diesel_derive_enum (in models.rs and main.rs). Nothing seems to make a difference, but I'm sure I'm just missing something obvious.

Edit: Full error message is:

  1. error[E0433]: failed to resolve: unresolved import
  2. --> src/database/models.rs:8:35
  3. |
  4. 8 | #[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)]
  5. | ^^^^^^^^^^^^^^^^^^^^^^^^^^
  6. | |
  7. | unresolved import
  8. | help: a similar path exists: `crate::database::schema`
  9. |
  10. = note: this error originates in the derive macro `diesel_derive_enum::DbEnum` (in Nightly builds, run with -Z macro-backtrace for more info)

答案1

得分: 0

  1. #[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)] // <- error is here
  2. #[ExistingTypePath = "crate::database::schema::sql_types::SupplyType"]
  3. pub enum SupplyType {
  4. Scarce,
  5. Limited,
  6. Moderate,
  7. Abundant,
  8. }
英文:

The derive macro itself is resolved correctly, but its expansion includes crate::schema::sql_types::SupplyType. The problem is, schema is not directly under the crate root - it is under database. So change it to:

  1. #[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)] // <- error is here
  2. #[ExistingTypePath = "crate::database::schema::sql_types::SupplyType"]
  3. pub enum SupplyType {
  4. Scarce,
  5. Limited,
  6. Moderate,
  7. Abundant,
  8. }

huangapple
  • 本文由 发表于 2023年5月11日 05:39:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定