导入不同目录中的函数

发布于 2025-01-20 08:04:40 字数 1933 浏览 4 评论 0 原文

我在从其他目录中导入功能中的lib.r.rs遇到困难。我在下面发布了代码和文件结构。

但是,当我将get_users.rs放入路由中时,将 pub mod get_users; 添加到 src/doutes/mod.rs.rs 服务器旋转并get用户工作。

文件结构

.
├── Cargo.lock
├── Cargo.toml
├── README.md
├── src
│   ├── lib.rs
│   ├── main.rs
│   └── routes
│       ├── echo.rs
│       └── mod.rs
|       |__ users
|            |__ get_users.rs
|            |-- mod.rs      
├── static
│   └── abruzzo.png
└── tests
    └── basic_test.rs

错误:

error[E0433]: failed to resolve: could not find `users` in `routes`
  --> src\lib.rs:15:17
   |
15 |         routes::users::get_users::get_users
   |                 ^^^^^ could not find `users` in `routes`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `rocket-mongo` due to previous error

main.rs

use rocket_mongo::rocket_builder;

fn main() {
    crate::rocket_builder().launch();
}

lib.rs

#![feature(proc_macro_hygiene, decl_macro)]
#![allow(unused_attributes)]

#[macro_use] use rocket::*;
use rocket_contrib::helmet::SpaceHelmet;

mod routes;

pub fn rocket_builder() -> rocket::Rocket {
    rocket::ignite().attach(SpaceHelmet::default())
    .mount("/", routes![
        routes::echo::echo_fn,
        routes::ping::ping_fn,
        routes::users::get_users::get_users
    ])
}

src/doutes/uste/users/get_users.rs

use rocket::*;

#[get("/users")]
pub fn get_users() -> String {
    "List of users".to_string()
}

src/routes/unders/mod.rs

pub mod get_users;

我尝试过的事情:

  • 添加 mod routes :: users; < lib.rs
  • 添加使用路由::用户:get_users;

edit:我不知道如何回复评论,但是jakubdóka回答了我,说我需要添加 pub mod用户; 在我的src/routes/mod.rs文件中。非常感谢Jakub!

I am having trouble importing function from a different directory into my lib.rs in rust. I posted my code and file structure below.

However, when I put the get_users.rs into routes and add pub mod get_users; to src/routes/mod.rs the server spins up and the get users work.

File Structure

.
├── Cargo.lock
├── Cargo.toml
├── README.md
├── src
│   ├── lib.rs
│   ├── main.rs
│   └── routes
│       ├── echo.rs
│       └── mod.rs
|       |__ users
|            |__ get_users.rs
|            |-- mod.rs      
├── static
│   └── abruzzo.png
└── tests
    └── basic_test.rs

Error:

error[E0433]: failed to resolve: could not find `users` in `routes`
  --> src\lib.rs:15:17
   |
15 |         routes::users::get_users::get_users
   |                 ^^^^^ could not find `users` in `routes`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `rocket-mongo` due to previous error

main.rs

use rocket_mongo::rocket_builder;

fn main() {
    crate::rocket_builder().launch();
}

lib.rs

#![feature(proc_macro_hygiene, decl_macro)]
#![allow(unused_attributes)]

#[macro_use] use rocket::*;
use rocket_contrib::helmet::SpaceHelmet;

mod routes;

pub fn rocket_builder() -> rocket::Rocket {
    rocket::ignite().attach(SpaceHelmet::default())
    .mount("/", routes![
        routes::echo::echo_fn,
        routes::ping::ping_fn,
        routes::users::get_users::get_users
    ])
}

src/routes/users/get_users.rs

use rocket::*;

#[get("/users")]
pub fn get_users() -> String {
    "List of users".to_string()
}

src/routes/users/mod.rs

pub mod get_users;

Things I have tried:

  • Adding mod routes::users; to lib.rs
  • Adding use routes::users:get_users;

Edit: I dont know how to reply to comments but Jakub Dóka answered me by saying I needed to add pub mod users; in my src/routes/mod.rs file. Thank you so much Jakub!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文