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 { pubmod greetings { pubfnhello() { println!("Hello, world!"); } }}fnmain() { phrases::greetings::hello(); // using full path}// -- Usage of use keyword --// 01. create alias for moduleuse phrases::greetings;fnmain() { greetings::hello();}// 02. create alias for module elementsuse phrases::greetings::hello;fnmain() { hello();}// 03. customize names with as keyworduse phrases::greetings::hello as greet;fnmain() { 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.
fnhello() ->String {"Hello, world!".to_string()}#[cfg(test)]mod tests {use super::hello; //import hello() into scope #[test]fntest_hello() {assert_eq!("Hello, world!", hello()); //if not using above use statement, we can run same via super::hello() }}
💡 By default, use declarations use absolute paths, starting from the crate root. But self and super declarations 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.
// -- 01. importing elements --use std::fs::File;fnmain() {File::create("empty.txt").expect("Can not create the file!");}// -- 02. importing module and elements--std::fs::{self, File} //use std::fs; use std::fs::File;fnmain() { fs::create_dir("some_dir").expect("Can not create the directry!");File::create("some_dir/empty.txt").expect("Can not create the file!");}// -- 03. importing multiple elementsuse std::fs::File;use std::io::{BufReader, BufRead}; //use std::io::BufReader; use std::io::BufRead;fnmain() {let file =File::open("src/hello.txt").expect("file not found");let buf_reader =BufReader::new(file);for line in buf_reader.lines() {println!("{}", line.unwrap()); }}
We don’t need to use extern crate std; when using std library. 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.