我有一个Cargo项目,由同一目录中的三个文件组成:main.rs
、mod1.rs
和mod2.rs
.
我想将函数从mod2.rs
导入到mod1.rs
,就像我将函数从mod1.rs
导入到main.rs
一样
有没有一种方法可以独立于目录 struct 导入/包含文件,就像我在Python或C++中那样?
主要的rs:
mod mod1; // Works
fn main() {
println!("Hello, world!");
mod1::mod1fn();
}
mod1.rs:
mod mod2; // Fails
pub fn mod1fn() {
println!("1");
mod2::mod2fn();
}
mod2.rs:
pub fn mod2fn() {
println!("2");
}
建筑的结果是:
error: cannot declare a new module at this location
--> src\mod1.rs:1:5
|
1 | mod mod2;
| ^^^^
|
note: maybe move this module `src` to its own directory via `src/mod.rs`
--> src\mod1.rs:1:5
|
1 | mod mod2;
| ^^^^
note: ... or maybe `use` the module `mod2` instead of possibly redeclaring it
--> src\mod1.rs:1:5
|
1 | mod mod2;
| ^^^^
我不能使用它,因为它在任何地方都不作为模块存在,而且我不想修改目录 struct .