diff options
| author | Adam Brewer <adamhb321@gmail.com> | 2023-10-16 07:37:12 -0400 |
|---|---|---|
| committer | Adam Brewer <adamhb321@gmail.com> | 2023-10-16 07:37:12 -0400 |
| commit | 64d95837e9813541cf5b357de13865ce687ae98d (patch) | |
| tree | f022c5d5ba01128811c0b77618a7adb843ee876b /exercises/10_modules | |
| parent | c3941323e2c0b9ee286494327de92e00f23b9e3a (diff) | |
Update Exercises Directory Names to Reflect Order
Diffstat (limited to 'exercises/10_modules')
| -rw-r--r-- | exercises/10_modules/README.md | 7 | ||||
| -rw-r--r-- | exercises/10_modules/modules1.rs | 22 | ||||
| -rw-r--r-- | exercises/10_modules/modules2.rs | 34 | ||||
| -rw-r--r-- | exercises/10_modules/modules3.rs | 21 |
4 files changed, 84 insertions, 0 deletions
diff --git a/exercises/10_modules/README.md b/exercises/10_modules/README.md new file mode 100644 index 0000000..3dc8a48 --- /dev/null +++ b/exercises/10_modules/README.md @@ -0,0 +1,7 @@ +# Modules + +In this section we'll give you an introduction to Rust's module system. + +## Further information + +- [The Module System](https://doc.rust-lang.org/book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html) diff --git a/exercises/10_modules/modules1.rs b/exercises/10_modules/modules1.rs new file mode 100644 index 0000000..9eb5a48 --- /dev/null +++ b/exercises/10_modules/modules1.rs @@ -0,0 +1,22 @@ +// modules1.rs +// +// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a +// hint. + +// I AM NOT DONE + +mod sausage_factory { + // Don't let anybody outside of this module see this! + fn get_secret_recipe() -> String { + String::from("Ginger") + } + + fn make_sausage() { + get_secret_recipe(); + println!("sausage!"); + } +} + +fn main() { + sausage_factory::make_sausage(); +} diff --git a/exercises/10_modules/modules2.rs b/exercises/10_modules/modules2.rs new file mode 100644 index 0000000..0415454 --- /dev/null +++ b/exercises/10_modules/modules2.rs @@ -0,0 +1,34 @@ +// modules2.rs +// +// You can bring module paths into scopes and provide new names for them with +// the 'use' and 'as' keywords. Fix these 'use' statements to make the code +// compile. +// +// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a +// hint. + +// I AM NOT DONE + +mod delicious_snacks { + // TODO: Fix these use statements + use self::fruits::PEAR as ??? + use self::veggies::CUCUMBER as ??? + + mod fruits { + pub const PEAR: &'static str = "Pear"; + pub const APPLE: &'static str = "Apple"; + } + + mod veggies { + pub const CUCUMBER: &'static str = "Cucumber"; + pub const CARROT: &'static str = "Carrot"; + } +} + +fn main() { + println!( + "favorite snacks: {} and {}", + delicious_snacks::fruit, + delicious_snacks::veggie + ); +} diff --git a/exercises/10_modules/modules3.rs b/exercises/10_modules/modules3.rs new file mode 100644 index 0000000..f2bb050 --- /dev/null +++ b/exercises/10_modules/modules3.rs @@ -0,0 +1,21 @@ +// modules3.rs +// +// You can use the 'use' keyword to bring module paths from modules from +// anywhere and especially from the Rust standard library into your scope. Bring +// SystemTime and UNIX_EPOCH from the std::time module. Bonus style points if +// you can do it with one line! +// +// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a +// hint. + +// I AM NOT DONE + +// TODO: Complete this use statement +use ??? + +fn main() { + match SystemTime::now().duration_since(UNIX_EPOCH) { + Ok(n) => println!("1970-01-01 00:00:00 UTC was {} seconds ago!", n.as_secs()), + Err(_) => panic!("SystemTime before UNIX EPOCH!"), + } +} |
