从main.rs以外的另一个.rs文件中使用Diesel的生成模式模块。

问题描述 投票:0回答:1

我对Rust真的非常非常陌生。我读了整本Rust的书,所以把理论弄懂了,但现在我想把它付诸实践。

我正在构建一个使用Diesel访问MySql数据库的网络服务。所有的设置都是正确的。Diesel成功地生成了 schema.rs 文件的内容反映了我的数据库模式。

table! {
  user (id) {
    ...
  }
}

我创建了一个 store.rs 旁边的文件。main.rs. 如果我对模块的理解是正确的,那么我把任何代码放在 store.rs 文件,将属于一个名为 store 嫡系 crate 模块。我的目的是把所有与数据库有关的代码放在 store 模块。然而,我似乎无法将 use 的东西 schema 在我 store 模块开始使用Diesel APIs做一些查询。

我试过use schema;, use crate::schema;, use super::schema;, use super::schema::user;但是什么都没有用。编译器总是说不能解析路径中的某一块或另一块。

Rust中引用兄弟模块的正确方法是什么?

module rust schema rust-diesel
1个回答
-1
投票

希望这个能帮到你。

在store.rs中

use crate::schema::*;

// … any other diesel related code you want to put here

in main.rs

pub mod schema;
mod store;
© www.soinside.com 2019 - 2024. All rights reserved.