Modules

modules1.rs

// TODO: Fix the compiler error about calling a private function.
mod sausage_factory {
    // Don't let anybody outside of this module see this!
    fn get_secret_recipe() -> String {
        String::from("Ginger")
    }

    pub fn make_sausage() {
        get_secret_recipe();
        println!("sausage!");
    }
}

fn main() {
    sausage_factory::make_sausage();
}
  • This exercise is simple we just need to make the make_sausage function accessible from main function but not the get_secret_recipe function.

  • We can do this by adding pub before fn make_sausage().

modules2.rs

  • In this exercise the TODO stated that we need to complete the use syntax below:

  • So we can conclude that we need to re-export the constant PEAR as fruit and CUCUMBER as veggie.

  • Don forget to add pub so it's accessible in main function.

modules3.rs

  • In this exercise we need to bring SystemTime and UNIX_EPOCH into main scope.

  • We can do this by using use like below:

Last updated