我有一个Cargo项目,由同一目录下的三个文件main.rs
、mod1.rs
和mod2.rs
组成。
我想要像在main.rs
中导入mod1.rs
中的函数一样,从mod2.rs
中导入函数到mod1.rs
中。
我已经阅读了所需的文件结构,但是我不理解 - 将所有导入的文件命名为mod
将会导致编辑器中的轻微混淆,而且这只是让项目层次更加复杂。
是否有一种可独立于目录结构导入/包含文件的方式,就像在Python或C++中一样?
main.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;
| ^^^^
我无法使用
它,因为它在任何地方都不存在作为一个模块,而且我不想修改目录结构。