use
Let's see the main usages of use keyword.
01. Bind a full path to a new name
Mainly use keyword is used to bind a full path of an element to a new name. So user doesn’t want to repeat the full path each time.
// -- Initial code without use keyword --
mod phrases {
pub mod greetings {
pub fn hello() {
println!("Hello, world!");
}
}
}
fn main() {
phrases::greetings::hello(); // using full path
}
// -- Usage of use keyword --
// 01. create alias for module
use phrases::greetings;
fn main() {
greetings::hello();
}
// 02. create alias for module elements
use phrases::greetings::hello;
fn main() {
hello();
}
// 03. customize names with as keyword
use phrases::greetings::hello as greet;
fn main() {
greet();
}02. Import elements to scope
Another common usage of use is importing elements to scope. Remember that, this is also bit similar to creating alias and using it instead of using the full path.
💡 By default,
usedeclarations use absolute paths, starting from the crate root. Butselfandsuperdeclarations make that path relative to the current module.
Same way use keyword is used to import the elements of other crates including std , Rust’s Standard Library.
We don’t need to use
extern crate std;when usingstdlibrary. We will discuss more about this under Standard Library section.
💡 use statements import only what we’ve specified into the scope, instead of importing all elements of a module or crate. So it improves the efficiency of the program.
03. Re-exporting
Another special case is pub use. When creating a module, you can export things from another module into your module. So after that they can be accessed directly from your module. This is called re-exporting.
This pattern is quite common in large libraries. It helps to hide the complexity of the internal module structure of the library from users, because users don’t need to know/follow whole directory map of the elements of the library while working with them.
Last updated