mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-25 00:00:05 +03:00
feat(modules): update exercises, add modules3 (#822)
Co-authored-by: diannasoriel <mokou@fastmail.com>
This commit is contained in:
parent
96fc301764
commit
dfd2fab4f3
|
@ -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 |
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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!");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
|
|
18
exercises/modules/modules3.rs
Normal file
18
exercises/modules/modules3.rs
Normal file
|
@ -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
info.toml
18
info.toml
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue