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

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

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:

src
├── database
│   ├── models.rs
│   ├── mod.rs
│   └── schema.rs
└── main.rs

Models.rs

use diesel::pg::PgConnection;
use diesel::prelude::*;
use diesel_derive_enum;
use uuid::Uuid;

use super::schema::market_trade_goods;

#[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)] // <- error is here
#[ExistingTypePath = "crate::schema::sql_types::SupplyType"]
pub enum SupplyType {
    Scarce,
    Limited,
    Moderate,
    Abundant,
}
// ...then more

database/mod.rs

pub mod models;
pub mod schema;

main.rs

mod database;

use diesel::pg::PgConnection;
use diesel::prelude::*;
use dotenvy::dotenv;
use std::env;
use uuid::Uuid;

use crate::database::models::{SupplyType, TradeGood};
// ...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:

error[E0433]: failed to resolve: unresolved import
 --> src/database/models.rs:8:35
  |
8 | #[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)]
  |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |                                   |
  |                                   unresolved import
  |                                   help: a similar path exists: `crate::database::schema`
  |
  = 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:

src
├── database
│   ├── models.rs
│   ├── mod.rs
│   └── schema.rs
└── main.rs

Models.rs

use diesel::pg::PgConnection;
use diesel::prelude::*;
use diesel_derive_enum;
use uuid::Uuid;

use super::schema::market_trade_goods;

#[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)] // <- error is here
#[ExistingTypePath = "crate::schema::sql_types::SupplyType"]
pub enum SupplyType {
    Scarce,
    Limited,
    Moderate,
    Abundant,
}
// ...then more

database/mod.rs

pub mod models;
pub mod schema;

main.rs

mod database;

use diesel::pg::PgConnection;
use diesel::prelude::*;
use dotenvy::dotenv;
use std::env;
use uuid::Uuid;

use crate::database::models::{SupplyType, TradeGood};
// ...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:

error[E0433]: failed to resolve: unresolved import
 --> src/database/models.rs:8:35
  |
8 | #[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)]
  |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |                                   |
  |                                   unresolved import
  |                                   help: a similar path exists: `crate::database::schema`
  |
  = 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

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

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:

#[derive(Debug, PartialEq, Clone, diesel_derive_enum::DbEnum)] // <- error is here
#[ExistingTypePath = "crate::database::schema::sql_types::SupplyType"]
pub enum SupplyType {
    Scarce,
    Limited,
    Moderate,
    Abundant,
}

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:

确定