Skip to content

Commit

Permalink
feat(modules): update exercises, add modules3 (#822)
Browse files Browse the repository at this point in the history
Co-authored-by: diannasoriel <mokou@fastmail.com>
  • Loading branch information
anuk909 and shadows-withal authored Sep 3, 2021
1 parent 96fc301 commit dfd2fab
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion exercises/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
| primitive_types | §4.3 |
| structs | §5.1 |
| enums | §6 |
| modules | §7.2 |
| modules | §7 |
| collections | §8.1, §8.3 |
| strings | §8.2 |
| error_handling | §9 |
Expand Down
2 changes: 1 addition & 1 deletion exercises/modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ 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-02-defining-modules-to-control-scope-and-privacy.html)
- [The Module System](https://doc.rust-lang.org/book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html)
6 changes: 6 additions & 0 deletions exercises/modules/modules1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
// 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!");
}
}
Expand Down
8 changes: 6 additions & 2 deletions exercises/modules/modules2.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// modules2.rs
// You can bring module paths into scopes and provide new names for them with the
// 'use' and 'as' keywords. Fix these 'use' statments to make the code compile.
// Make me compile! Execute `rustlings hint modules2` for hints :)

// I AM NOT DONE

mod delicious_snacks {
use self::fruits::PEAR as fruit;
use self::veggies::CUCUMBER as veggie;

// TODO: Fix these use statments
use self::fruits::PEAR as ???
use self::veggies::CUCUMBER as ???

mod fruits {
pub const PEAR: &'static str = "Pear";
Expand Down
18 changes: 18 additions & 0 deletions exercises/modules/modules3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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!
// Make me compile! Execute `rustlings hint modules3` for hints :)

// 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!"),
}
}
18 changes: 13 additions & 5 deletions info.toml
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,19 @@ name = "modules2"
path = "exercises/modules/modules2.rs"
mode = "compile"
hint = """
The delicious_snacks module is trying to present an external
interface (the `fruit` and `veggie` constants) that is different than
its internal structure (the `fruits` and `veggies` modules and
associated constants). It's almost there except for one keyword missing for
each constant."""
The delicious_snacks module is trying to present an external interface that is
different than its internal structure (the `fruits` and `veggies` modules and
associated constants). Complete the `use` statemants to fit the uses in main and
find the one keyword missing for both constants."""

[[exercises]]
name = "modules3"
path = "exercises/modules/modules3.rs"
mode = "compile"
hint = """
UNIX_EPOCH and SystemTime are declared in the std::time module. Add a use statement
for these two to bring them into scope. You can use nested paths or the glob
operator to bring these two in using only one line."""

# COLLECTIONS

Expand Down

0 comments on commit dfd2fab

Please sign in to comment.