[Rust] 模块与文件分离
我们可以让模块不再平铺在目录里面,把他移入文件文件夹中。
src
├── front_of_house
│ └── hosting.rs
├── front_of_house.rs
└── lib.rs//lib.rs
mod front_of_house;//导入当前目录下的front_of_house.rs文件
pub use crate::front_of_house::hosting;//导入其中的方法
pub fn eat_at_restaurant() {
hosting::add_to_waitlist();
hosting::add_to_waitlist();
hosting::add_to_waitlist();
}//front_of_house.rs与模块名同名文件夹下的方法名.rs文件
pub mod hosting; //导入文件 文件夹里有多少可以导入多少//front_of_house/hosting.rs
pub fn add_to_waitlist() {
//具体实现
}[Rust] 模块与文件分离
https://blog.jiang.in/archives/83b71fbc-5fa4-4e6c-ac36-2c84d6f1fbdd