如何从上述文件夹中的另一个文件夹导入函数



我的目录结构是这样的

src
├── main.rs
├── paas_core
│   ├── create.rs
│   └── mod.rs
└── rest_api
├── mod.rs
└── rest.rs

我想使用在文件create.rs中声明的函数在文件rest.rs中。我试图在rest_api模块中使用该模块,但我不知道如何做到这一点。

我尝试使用super::paas_corerest.rs,但没有工作。

在文件rest_api/rest.rs中,您是否必须使用use crate::paas_core;以便能够从rest_api引用到paas_core,paas_core/mod.rs需要语句pub mod create;以确保您可以直接使用paas_core::create::create_func();从rest_api调用函数

我希望它能解释它。完整的示例可能如下所示:

文件:main.rs

mod paas_core;
mod rest_api;
fn main() {
rest_api::rest_func();
}

文件:rest_api/mod.rs


mod rest;
pub fn rest_func() {
println!("rest_func() in rest_api/mod.rs called!");
rest::rest_caller();
}

文件:rest_api/rest.rs

use crate::paas_core;
pub fn rest_caller() {
println!("rest_caller() in rest_api/rest.rs called!");
paas_core::create::create_func();
}

文件:paas_core/mod.rs

pub mod create;

文件:paas_core/create.rs

pub fn create_func() {
println!("create_func() in paas_core/create.rs finally called!");
}

输出:

rest_func() in rest_api/mod.rs called!
rest_caller() in rest_api/rest.rs called!
create_func() in paas_core/create.rs finally called!

相关内容

最新更新