Per Rust Programming language-online book, Section 7.3: * A relative path starts from the current module and uses self
, super
, or an identifier in the current module.
The example there,
mod front_of_house {
pub mod hosting {
fn add_to_waitlist() {}
}
}
When calling fn add_to_waitlist(), the relative path is shown as,
// Relative path
front_of_house::hosting::add_to_waitlist();
Should it not be, hosting::add_to_waitlist(), as the current module is hosting not the front_of_house?
Would appreciate response.
Thanks!
It depends on where are you calling it from. That is, if you are calling it from the front_of_house
module, then you would call it as hosting::add_to_waitlist()
otherwise you would use the full path.
Thanks for the explanation! The books says, relative path starts from the current module. My thinking is that the current module is the hosting. So I am confused.
That's correct. Here's an example that might help you understand:
fn main() {
mod front_of_house {
pub mod hosting {
pub fn add_to_waitlist() {
println!("Called!")
}
pub fn same_module() {
add_to_waitlist()
}
}
pub mod other_module {
fn example() {
super::hosting::add_to_waitlist()
}
}
}
}