[Rust] 模块与文件分离

2025-02-23

我们可以让模块不再平铺在目录里面,把他移入文件文件夹中。

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() {
    //具体实现
}

PREV
[Rust] Rust语言圣经
NEXT
[autofit.js] 可视化大屏 一行搞定自适应