summaryrefslogtreecommitdiff
path: root/exercises/10_modules
diff options
context:
space:
mode:
Diffstat (limited to 'exercises/10_modules')
-rw-r--r--exercises/10_modules/README.md7
-rw-r--r--exercises/10_modules/modules1.rs22
-rw-r--r--exercises/10_modules/modules2.rs34
-rw-r--r--exercises/10_modules/modules3.rs21
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!"),
+ }
+}