我有以下代码:
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
);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试打印水果和蔬菜时,我收到一个错误:水果和蔬菜是私有的:
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
);
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以向我解释这一点并帮助我解决吗?
定义的项目use的可访问性独立于所用项目的可访问性。这意味着 whilePEAR是公共的,use self::fruits::PEAR as fruit;是私有的。您必须公开导出这些项目:
pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;
Run Code Online (Sandbox Code Playgroud)
(固定链接到操场)