Paths for Referring to an Item in the Module Tree is a way to show Rust where to find an item in a module tree. This is done by using a path, which is a sequence of identifiers separated by double colons (::).
There are two types of paths: absolute and relative. An absolute path starts from the crate root, while a relative path starts from the current module. Here is an example of a path for the add_to_waitlist function:
crate::front_of_house::hosting::add_to_waitlist
This path is absolute because it starts from the crate root. The crate keyword tells Rust to start from the root of the current crate. The front_of_house and hosting identifiers refer to modules, and the add_to_waitlist identifier refers to a function. Here is an example of a relative path for the add_to_waitlist function:
front_of_house::hosting::add_to_waitlist
This path is relative because it starts from the current module, which is front_of_house. The hosting and add_to_waitlist identifiers refer to modules and a function, respectively. Paths can be used to call functions, access variables, and refer to other items in the module tree. Here is a new example of using paths:
mod front_of_house {
mod hosting {
pub fn add_to_waitlist() {}
}
}
fn main() {
// Absolute path
crate::front_of_house::hosting::add_to_waitlist();
// Relative path
front_of_house::hosting::add_to_waitlist();
}
This code will compile and run without errors. The paths for the add_to_waitlist function are correct, and the pub keyword makes the add_to_waitlist function public, so it can be accessed from other modules.