如何在Rust中使用多个文件?
我的主要代码在
文件夹结构:
我收到了这个错误信息:
file1.rs
中。file2.rs
多次运行file1.rs
的主函数,这就是为什么我想把它拆分成两个文件来保持我的代码整洁。然后我只想在main.rs
文件中运行file2.rs
的主函数。(我正在使用最新版本的rust-2021)文件夹结构:
├── Cargo.lock
├── Cargo.toml
├── src
│ ├── main.rs
│ └── file1.rs
| └── file2.rs
└── target
main.rs
pub mod file1;
pub mod file2;
pub fn main() {
file2::main();
}
file2.rs
pub mod file1;
pub fn main() {
file1::func("Bob");
file1::func("Alice");
}
file1.rs
pub fn func(name: &str) {
println!("Hello {}", name.to_string());
}
我收到了这个错误信息:
file not found for module `file1`
to create the module `file1`, create file "src/file2/file1.rs"
or "src/file2/file1/mod.rs" rustcE0583
file1
中将函数名更改为 func。但是导入错误仍然存在... - hups