[Rustlings] module2 problem

Hello,

I have this code :

// I AM NOT DONE

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

    pub mod fruits {
        pub const PEAR: &'static str = "Pear";
        pub const APPLE: &'static str = "Apple";
    }

    pub 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
    );
}

errror :

! Compiling of exercises/modules/modules2.rs failed! Please try again. Here's the output:
error[E0603]: constant import `fruit` is private
  --> exercises/modules/modules2.rs:24:27
   |
24 |         delicious_snacks::fruit,
   |                           ^^^^^ private constant import
   |
note: the constant import `fruit` is defined here...
  --> exercises/modules/modules2.rs:7:9
   |
7  |     use self::fruits::PEAR as fruit;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...and refers to the constant `PEAR` which is defined here
  --> exercises/modules/modules2.rs:11:9
   |
11 |         pub const PEAR: &'static str = "Pear";
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly

error[E0603]: constant import `veggie` is private
  --> exercises/modules/modules2.rs:25:27
   |
25 |         delicious_snacks::veggie
   |                           ^^^^^^ private constant import
   |
note: the constant import `veggie` is defined here...
  --> exercises/modules/modules2.rs:8:9
   |
8  |     use self::veggies::CUCUMBER as veggie;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...and refers to the constant `CUCUMBER` which is defined here
  --> exercises/modules/modules2.rs:16:9
   |
16 |         pub const CUCUMBER: &'static str = "Cucumber";
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ consider importing it directly

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0603`.

Can somenone explain what I have done wrong ?

1 Like

You need to make the re-exports public:

pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;
1 Like

oke,

so this is good rust code:

// modules2.rs

// Make me compile! Execute `rustlings hint modules2` for hints :)

// I AM NOT DONE

mod delicious_snacks {

   pub use self::fruits::PEAR as fruit;

   pub use self::veggies::CUCUMBER as veggie;

   pub mod fruits {

       pub const PEAR: &'static str = "Pear";

       pub const APPLE: &'static str = "Apple";

   }

   pub 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

   );

}
1 Like

Well, it's not clear what do you want to do here. Currently, you can just write the following:

fn main() {
   println!(
       "favorite snacks: {} and {}",
       delicious_snacks::fruits::PEAR,
       delicious_snacks::veggies::CUCUMBER,
   );
}

because both mod fruits and mod veggies are public, and so accessible from the top level without pub useing their contents.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.