Reputation: 414
I have the following code:
mod delicious_snacks {
use self::fruits::PEAR as fruit;
use self::veggies::CUCUMBER as veggie;
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
);
}
I'm getting an error that fruit and veggie are private when I try to print them:
error[E0603]: constant `fruit` is private
--> src/main.rs:19:31
|
19 | delicious_snacks::fruit,
| ^^^^^
error[E0603]: constant `veggie` is private
--> src/main.rs:20:31
|
20 | delicious_snacks::veggie
| ^^^^^^
Can any one explain me this and help me resolve?
Upvotes: 7
Views: 3958
Reputation: 1
To use
pub mod fruits {
and
println!(
"favorite snacks: {}",
delicious_snacks::fruits::PEAR,
);
is another way to address problem
Upvotes: -1
Reputation: 30061
The accessibility of an item defined with use
is independent from the accessibility of the used item. This means that while PEAR
is public, use self::fruits::PEAR as fruit;
is private. You must export those items publicly:
pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;
Upvotes: 15
Reputation: 42959
It means it is only visible from the delicious_snacks
module.
To use delicious_snacks::fruit
and the other constants, they need to be exported publicly from the module using the pub
modifier:
mod delicious_snacks {
pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;
Upvotes: 1