// in lib.rs
pub mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
pub fn seat_at_table() -> String {
String::from(“sit down please”)
}
}
pub mod serving {
pub fn take_order() {}
pub fn serve_order() {}
pub fn take_payment() {}
// 我猜你不希望顾客听到你在抱怨他们,因此让这个函数私有化吧
fn complain() {}
}
}
pub fn eat_at_restaurant() -> String {
front_of_house::hosting::add_to_waitlist();
back_of_house::cook_order();
String::from(“yummy yummy!”)
}
pub mod back_of_house {
pub fn fix_incorrect_order() {
cook_order();
crate::front_of_house::serving::serve_order();
}
pub fn cook_order() {}
}
4. 🌟🌟🌟🌟 请将上面的模块和代码分离到以下目录文件中e :
.
├── Cargo.toml
├── src
│ ├── back_of_house.rs
│ ├── front_of_house
│ │ ├── hosting.rs
│ │ ├── mod.rs
│ │ └── serving.rs
│ ├── lib.rs
│ └── main.rs
// in src/lib.rs
// IMPLEMENT…
// in src/back_of_house.rs
// IMPLEMENT…
// in src/front_of_house/mod.rs
// IMPLEMENT…
// in src/front_of_house/hosting.rs
// IMPLEMENT…
// in src/front_of_house/serving.rs
// IMPLEMENT…
从二进制包中访问库包的代码
请确保你已经完成了第四题,然后再继续进行.
当到底此处时,你的项目结构应该如下所示:
.
├── Cargo.toml
├── src
│ ├── back_of_house.rs
│ ├── front_of_house
│ │ ├── hosting.rs
│ │ ├── mod.rs
│ │ └── serving.rs
│ ├── lib.rs
│ └── main.rs
5. 🌟🌟🌟现在我们可以从二进制包中发起函数调用了.
// in src/main.rs
// 填 空 并 修 复 错 误
fn main() {
assert_eq!(, “sit down please”);
assert_eq!(,“yummy yummy!”);
}
Rust从入门到实战系列三百一十:将模块分离并放入独立的文件中
最新推荐文章于 2024-08-13 20:04:34 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)