rust - 使用未声明的箱子或模块, "use crate_name::schema::posts"并不总是有效

标签 rust actix-web

我正在尝试通过与 actix-web 一起使用 Rust 来学习它。和 diesel .
当我尝试通过使用 crate 名称导入/使用模式时,它仅适用于 example.rs文件但不在 post.rs 中文件。这两个文件都嵌套在自己的文件夹中,我使用的命令如下:use web_project_core::schema::posts;当我改用这个其他命令时,它在 post.rs 中工作但不在 example.rs 中:use super::super::schema::posts;我错过了什么?

// Cargo.toml
[lib]
name = "web_project_core"
path = "src/lib.rs"

[[bin]]
name = "main"
path = "src/main.rs"
// main.rs

use actix_web::{App, HttpServer};

mod handlers;
// lib.rs
#[macro_use]
extern crate diesel;
extern crate dotenv;

use diesel::prelude::*;
use diesel::pg::PgConnection;
use dotenv::dotenv;
use std::env;

pub mod schema;
pub mod modelz;
// post.rs

use serde::{Serialize, Deserialize};
use nanoid::generate;

use super::super::schema::posts;        // <-- it works
// use web_project_core::schema::posts; // <-- it doesn't work
// example.rs

use actix_web::{get, web, post, HttpResponse, Responder};
use diesel::prelude::*;

use web_project_core::establish_connection;
use web_project_core::schema::posts;            // <-- it works
// use super::super::schema::posts;             // <-- it doesn't work
use web_project_core::modelz::post::*;
项目结构:
Project structure
谢谢

最佳答案

example.rs 之间的区别和 post.rspost.rs在图书馆的箱子里 web_project_coreexample.rs在二进制包中 main .
路径web_project_core::schema::postspost.rs 中不可用如web_project_core是当前的 crate,而不是依赖项。
而不是 web_project_core::schema::posts你可以使用 crate::schema::posts , 当指的是当前的箱子时。二进制文件隐式依赖于库,因为它们位于同一个包中,路径为 web_project_core::schema::posts可用 example.rs

关于rust - 使用未声明的箱子或模块, "use crate_name::schema::posts"并不总是有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65787479/

相关文章:

rust - 为什么我共享的 actix-web 状态有时会重置回原始值?

rust - 入门Actix-web : failed to run custom build command for `brotli-sys v0.3.2` 时出错

error-handling - 如何使用Serde解析可能在不失败整个反序列化的情况下无法反序列化的字段?

rust - Rust 如何处理将 &SizedType 转换为 &UnsizedType?

types - 如何 unwrap_or 到 String

logging - 您如何在代码的更深处处理记录 HTTP 请求以及输出跟踪和错误消息等不同任务?

rust - 如何高效地使用 Actix Multipart 将单个文件上传到磁盘?

std - 使用哪个 std::sync::atomic::Ordering ?

macros - 是否可以让宏扩展为结构字段?

types - 如何创建同时接受sqlx数据库池和事务的actix-web服务器?