Add [Modules]

This commit is contained in:
sunface 2022-03-14 17:31:27 +08:00
parent d59db9aff7
commit c6c744c183
2 changed files with 340 additions and 0 deletions

View File

@ -0,0 +1,148 @@
1.
```rust
// in lib.rs
mod front_of_house {
mod hosting {
fn add_to_waitlist() {}
fn seat_at_table() {}
}
mod serving {
fn take_order() {}
fn serve_order() {}
fn take_payment() {}
fn complain() {}
}
}
```
2.
```rust
// in lib.rs
pub mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {}
pub fn seat_at_table() {}
}
pub mod serving {
pub fn take_order() {}
pub fn serve_order() {}
pub fn take_payment() {}
// Maybe you don't want the guest hearing the your complaining about them
// So just make it private
fn complain() {}
}
}
pub fn eat_at_restaurant() {
// 绝对路径
crate::front_of_house::hosting::add_to_waitlist();
// 相对路径
front_of_house::hosting::add_to_waitlist();
}
```
3.
```rust
mod back_of_house {
fn fix_incorrect_order() {
cook_order();
super::front_of_house::serving::serve_order();
}
fn cook_order() {}
}
```
```rust
mod back_of_house {
fn fix_incorrect_order() {
cook_order();
crate::front_of_house::serving::serve_order();
}
fn cook_order() {}
}
```
4.
```rust
// in src/lib.rs
mod front_of_house;
mod back_of_house;
pub fn eat_at_restaurant() -> String {
front_of_house::hosting::add_to_waitlist();
back_of_house::cook_order();
String::from("yummy yummy!")
}
```
```rust
// in src/back_of_house.rs
use crate::front_of_house;
pub fn fix_incorrect_order() {
cook_order();
front_of_house::serving::serve_order();
}
pub fn cook_order() {}
```
```rust
// in src/front_of_house/mod.rs
pub mod hosting;
pub mod serving;
```
```rust
// in src/front_of_house/hosting.rs
pub fn add_to_waitlist() {}
pub fn seat_at_table() -> String {
String::from("sit down please")
}
```
```rust,editable
// in src/front_of_house/serving.rs
pub fn take_order() {}
pub fn serve_order() {}
pub fn take_payment() {}
// Maybe you don't want the guest hearing the your complaining about them
// So just make it private
fn complain() {}
```
5.
```rust
mod front_of_house;
fn main() {
assert_eq!(front_of_house::hosting::seat_at_table(), "sit down please");
assert_eq!(hello_package::eat_at_restaurant(),"yummy yummy!");
}
```

View File

@ -1 +1,193 @@
# Module
Modules let us organize the code within a crate into groups by readablity and easy reuse. Module also controls the privacy of items, which is whether an item can be seen by outside code( public ), or is just an internal implementation and not available for outside code( private ).
We have created a package named `hello-package` in previous chapter, and it looks like this:
```shell
.
├── Cargo.toml
├── src
│   ├── lib.rs
│   └── main.rs
```
Now it's time to create some modules in the library crate and use them in the binary crate, let's start.
1. 🌟🌟 Implement module `front_of_house` based on the module tree below:
```shell
library crate root
└── front_of_house
├── hosting
│ ├── add_to_waitlist
│ └── seat_at_table
└── serving
├── take_order
├── serve_order
├── take_payment
└── complain
```
```rust,editable
// FILL in the blank
// in __.rs
mod front_of_house {
// IMPLEMENT this module..
}
```
2. 🌟🌟 Let's call `add_to_waitlist` from a function `eat_at_restaurant` which within the library crate root.
```rust
// in lib.rs
// FILL in the blanks and FIX the errors
// You need make something public with `pub` to provide accessiblity for outside code `fn eat_at_restaurant()`
mod front_of_house {
/* ...snip... */
}
pub fn eat_at_restaurant() {
// call add_to_waitlist with **absolute path**:
__.add_to_waitlist();
// call with **relative path**
__.add_to_waitlist();
}
```
3. 🌟🌟 You can use `super` to import items within the parent module
```rust,editable
// in lib.rs
mod back_of_house {
fn fix_incorrect_order() {
cook_order();
// FILL in the blank in tree ways
//1. using keyword `super`
//2. using absolute path
__.serve_order();
}
fn cook_order() {}
}
```
### Separating modules into different files
```rust
// 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() {}
// Maybe you don't want the guest hearing the your complaining about them
// So just make it private
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. 🌟🌟🌟🌟 Please separate the modules and codes above into files resident in below dir tree :
```shell
.
├── Cargo.toml
├── src
│   ├── back_of_house.rs
│   ├── front_of_house
│   │   ├── hosting.rs
│   │   ├── mod.rs
│   │   └── serving.rs
│   ├── lib.rs
│   └── main.rs
```
```rust,editable
// in src/lib.rs
// IMPLEMENT...
```
```rust,editable
// in src/back_of_house.rs
// IMPLEMENT...
```
```rust,editable
// in src/front_of_house/mod.rs
// IMPLEMENT...
```
```rust,editable
// in src/front_of_house/hosting.rs
// IMPLEMENT...
```
```rust,editable
// in src/front_of_house/serving.rs
// IMPLEMENT...
```
### accessing code in library crate from binary crate
**Please ensure you have completed the 4th exercise before making further progress.**
You should have below structures and the corresponding codes in them when reaching here:
```shell
.
├── Cargo.toml
├── src
│   ├── back_of_house.rs
│   ├── front_of_house
│   │   ├── hosting.rs
│   │   ├── mod.rs
│   │   └── serving.rs
│   ├── lib.rs
│   └── main.rs
```
5. 🌟🌟🌟 Now we will call a few library functions from the binary crate.
```rust,editable
// in src/main.rs
// FILL in the blank and FIX the errors
fn main() {
assert_eq!(__, "sit down please");
assert_eq!(__,"yummy yummy!");
}
```