From db1b2c9483dd871627bf5a70884db083a31fbeb4 Mon Sep 17 00:00:00 2001 From: My Chiffon Nguyen Date: Fri, 21 Jun 2024 15:46:30 +0200 Subject: [PATCH] 10 modules - pub to make stuff available for other modules - use ~ import - use [path] as ~ import [path] as --- exercises/10_modules/modules1.rs | 4 ++-- exercises/10_modules/modules2.rs | 18 ++++++++++-------- exercises/10_modules/modules3.rs | 6 +++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/exercises/10_modules/modules1.rs b/exercises/10_modules/modules1.rs index 9eb5a48b..6019f3f1 100644 --- a/exercises/10_modules/modules1.rs +++ b/exercises/10_modules/modules1.rs @@ -3,7 +3,7 @@ // Execute `rustlings hint modules1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE +// https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html mod sausage_factory { // Don't let anybody outside of this module see this! @@ -11,7 +11,7 @@ mod sausage_factory { String::from("Ginger") } - fn make_sausage() { + pub fn make_sausage() { // pub makes this function public get_secret_recipe(); println!("sausage!"); } diff --git a/exercises/10_modules/modules2.rs b/exercises/10_modules/modules2.rs index 04154543..4fb46384 100644 --- a/exercises/10_modules/modules2.rs +++ b/exercises/10_modules/modules2.rs @@ -7,19 +7,21 @@ // Execute `rustlings hint modules2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE +// https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html mod delicious_snacks { - // TODO: Fix these use statements - use self::fruits::PEAR as ??? - use self::veggies::CUCUMBER as ??? + // 'use' keyword creates shortcuts to items to reduce repetition of long paths + use self::fruits::PEAR as PEAR; + use self::veggies::CUCUMBER as CUCUMBER; - mod fruits { + // pub makes these constants public to be used in main() + // https://doc.rust-lang.org/book/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html#exposing-paths-with-the-pub-keyword + pub mod fruits { pub const PEAR: &'static str = "Pear"; pub const APPLE: &'static str = "Apple"; } - mod veggies { + pub mod veggies { pub const CUCUMBER: &'static str = "Cucumber"; pub const CARROT: &'static str = "Carrot"; } @@ -28,7 +30,7 @@ mod delicious_snacks { fn main() { println!( "favorite snacks: {} and {}", - delicious_snacks::fruit, - delicious_snacks::veggie + delicious_snacks::fruits::PEAR, + delicious_snacks::veggies::CUCUMBER ); } diff --git a/exercises/10_modules/modules3.rs b/exercises/10_modules/modules3.rs index f2bb0503..8b1cbfe0 100644 --- a/exercises/10_modules/modules3.rs +++ b/exercises/10_modules/modules3.rs @@ -8,10 +8,10 @@ // Execute `rustlings hint modules3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE +// https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#bringing-paths-into-scope-with-the-use-keyword -// TODO: Complete this use statement -use ??? +// Import SystemTime and UNIX_EPOCH from the std::time module +use std::time::{SystemTime, UNIX_EPOCH}; fn main() { match SystemTime::now().duration_since(UNIX_EPOCH) {