I have a Rust module breakfast containing two sub modules egg and bacon. The breakfast module must know about egg and bacon, but the two children do not need to and therefore should not know about each other.
This is what my code looks like now. The breakfast gets made, but unfortunately egg and bacon can access each other.
mod breakfast {
pub fn make_breakfast() -> String {
format!("{} and {}", egg::EGG, bacon::BACON)
}
mod egg {
pub const EGG: &'static str = "egg";
}
mod bacon {
pub const BACON: &'static str = "bacon";
// Oh no! The bacon knows about the egg!
// I want this to be a compile error.
use super::egg::EGG;
}
}
Can I hide the siblings from each other somehow, perhaps by using visibility modifiers or by restructuring the modules? Or is the unneeded visibility something I should accept?
In reality, the modules are in separate files, but I put them in one here to make a clearer example.
This is by design:
For further information on this, please consult the relevant chapter of The Reference