rust - 将 Diesel 方法移动到其他目录

标签 rust rust-diesel

我正在遵循 Diesel 示例指南,我的项目看起来完全是 like this .我想更改它,而不是运行 cargo run --bin publish_post 1,而是使用 cargo run 并显示一个循环,提示您执行您想要的操作跑。

我已将所有内容从 bin/ 移到 controllers/ 目录中。我想在 main.rs 中引用它作为 use controllers::post,所以我可以访问 post::delete() 等.

一旦我将文件移出 bin/,所有导入都会中断。同样,我无法从 lib.rs 中引用它。

为什么在移动文件后我的导入都不起作用?我如何从这些文件中访问方法?

我想要这样的结构:

├── controllers
│   └── posts.rs
├── lib.rs
├── main.rs
├── models.rs
├── schema.rs

main.rs 中,我希望能够执行如下操作:

use controllers::posts;

pub fn main() {
    // pseudocode
    loop {
        println!("what action would you like to perform?");
        let ans = capture_input();

        if ans == "insert" {
            posts::insert();
        } else if ans == "delete" {
            posts::delete();
        }
    }
}

最佳答案

创建文件夹不会自动创建 Rust 子模块。你需要做两件事:

  1. 在 crate root 中显式声明模块(lib.rsmain.rs):

    mod controllers;
    
  2. 创建 controllers/mod.rs 文件并在其中声明一个子模块:

    mod posts;
    

关于rust - 将 Diesel 方法移动到其他目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44810825/

相关文章:

function - 在闭包内调用堆上函数参数

rust - 如何在 Diesel 中为自定义字段类型实现可查询和可插入?

rust - 如何制作柴油自动生成模型

types - 我什么时候应该在普通元组上使用元组结构?

rust - 在 Rust 中使用 warp 框架同时在多个端口(http、https)上提供服务

regex - Rust 正则表达式模式 - 无法识别的转义模式

rest - rust + 火箭 : How I do read POST body from request as string?

rust - 在柴油塔中使用新类型

Rust diesel orm查询

rust - 为泛型编写 Diesel CRUD 操作