Skip to content

Commit

Permalink
10 modules
Browse files Browse the repository at this point in the history
- pub to make stuff available for other modules
- use ~ import
- use [path] as ~ import [path] as
  • Loading branch information
chiffonng committed Jun 21, 2024
1 parent 151d372 commit db1b2c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions exercises/10_modules/modules1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// 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!
fn get_secret_recipe() -> String {
String::from("Ginger")
}

fn make_sausage() {
pub fn make_sausage() { // pub makes this function public
get_secret_recipe();
println!("sausage!");
}
Expand Down
18 changes: 10 additions & 8 deletions exercises/10_modules/modules2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand All @@ -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
);
}
6 changes: 3 additions & 3 deletions exercises/10_modules/modules3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit db1b2c9

Please sign in to comment.