rust-by-practice/en/src/crate-module/module.md

4.3 KiB

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:

.
├── 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:
library crate root
 └── front_of_house
     ├── hosting
     │   ├── add_to_waitlist
     │   └── seat_at_table
     └── serving
         ├── take_order
         ├── serve_order
         ├── take_payment
         └── complain
// FILL in the blank
// in __.rs

mod front_of_house {
    // IMPLEMENT this module..
}
  1. 🌟🌟 Let's call add_to_waitlist from a function eat_at_restaurant which within the library crate root.
// 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();
}
  1. 🌟🌟 You can use super to import items within the parent module
// 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

// 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() {}
}
  1. 🌟🌟🌟🌟 Please separate the modules and codes above into files resident in below dir tree :
.
├── 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...

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:

.
├── Cargo.toml
├── src
│   ├── back_of_house.rs
│   ├── front_of_house
│   │   ├── hosting.rs
│   │   ├── mod.rs
│   │   └── serving.rs
│   ├── lib.rs
│   └── main.rs
  1. 🌟🌟🌟 Now we will call a few library functions from the binary crate.
// in src/main.rs

// FILL in the blank and FIX the errors
fn main() {
    assert_eq!(__, "sit down please");
    assert_eq!(__,"yummy yummy!");
}

You can find the solutions here (under the solutions path), but only use it when you need it :)