modules2 solution

This commit is contained in:
mo8it 2024-06-22 13:24:06 +02:00
parent ecbe9b7324
commit 98cd00de63
3 changed files with 38 additions and 16 deletions

View file

@ -1,20 +1,19 @@
// You can bring module paths into scopes and provide new names for them with // 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 // the `use` and `as` keywords.
// compile.
mod delicious_snacks { mod delicious_snacks {
// TODO: Fix these use statements // TODO: Add the follwing two `use` statements after fixing them.
use self::fruits::PEAR as ??? // use self::fruits::PEAR as ???;
use self::veggies::CUCUMBER as ??? // use self::veggies::CUCUMBER as ???;
mod fruits { mod fruits {
pub const PEAR: &'static str = "Pear"; pub const PEAR: &str = "Pear";
pub const APPLE: &'static str = "Apple"; pub const APPLE: &str = "Apple";
} }
mod veggies { mod veggies {
pub const CUCUMBER: &'static str = "Cucumber"; pub const CUCUMBER: &str = "Cucumber";
pub const CARROT: &'static str = "Carrot"; pub const CARROT: &str = "Carrot";
} }
} }
@ -22,6 +21,6 @@ fn main() {
println!( println!(
"favorite snacks: {} and {}", "favorite snacks: {} and {}",
delicious_snacks::fruit, delicious_snacks::fruit,
delicious_snacks::veggie delicious_snacks::veggie,
); );
} }

View file

@ -535,12 +535,13 @@ name = "modules2"
dir = "10_modules" dir = "10_modules"
test = false test = false
hint = """ hint = """
The delicious_snacks module is trying to present an external interface that is The `delicious_snacks` module is trying to present an external interface that
different than its internal structure (the `fruits` and `veggies` modules and is different than its internal structure (the `fruits` and `veggies` modules
associated constants). Complete the `use` statements to fit the uses in main and and associated constants). Complete the `use` statements to fit the uses in
find the one keyword missing for both constants. `main` and find the one keyword missing for both constants.
Learn more at https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#re-exporting-names-with-pub-use""" Learn more in The Book:
https://doc.rust-lang.org/book/ch07-04-bringing-paths-into-scope-with-the-use-keyword.html#re-exporting-names-with-pub-use"""
[[exercises]] [[exercises]]
name = "modules3" name = "modules3"

View file

@ -1 +1,23 @@
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰 mod delicious_snacks {
// Added `pub` and used the expected alias after `as`.
pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;
mod fruits {
pub const PEAR: &str = "Pear";
pub const APPLE: &str = "Apple";
}
mod veggies {
pub const CUCUMBER: &str = "Cucumber";
pub const CARROT: &str = "Carrot";
}
}
fn main() {
println!(
"favorite snacks: {} and {}",
delicious_snacks::fruit,
delicious_snacks::veggie,
);
}