Rust provides a mechanism for code encapsulation. Relatively independent module modules can be created by crate (equivalent to the package in Java), which encapsulates functional functions that can be reused.
When you create your own Lib library or when you want to use a Third-party library (which is some crate) you need to refer the module modules in these libraries to the current environment.
Rust provides several ways of referencing: first, use extern crate
In the files using these modules, the module within the package is introduced into the current environment through the extern crate + crate name.
extern crate My_library;
fn Main () {
println! (" Hello in 中文版: {} ", My_library::english::greetings::hello ());
println! ("Goodbye in 中文版: {}", My_library::english::farewells::goodbye ());
println! ("Hello in Chinese: {}", My_library::chinese::greetings::hello ());
println! ("Goodbye in Chinese: {}", My_library::chinese::farewells::goodbye ());
}
The Crate or class libraries introduced through extern crate need to pass the module modules that will need to be referenced by layers of two: double quotes, until the function function is invoked, for example:
My_library::english::farewells::goodbye ()
Here's what this line is all about. what relationship.
Ii. the relationship between crate, module and functional methods
My_library::english::farewells::goodbye ()
My_library is the name of the project that we created using cargo new, which is the name of the project's default module, which is the root module,module corresponding file is lib.rs.
So, when we declare pub mod 中文版 in lib.rs; , it produces such a relationship my_library::english;
Then the pub mod farewells is declared in src/english.rs or src/english/mod.rs, and there is such a relationship my_library::english::farewells
Finally, the method of the declaration is reached.
My_library::english::farewells::goodbye ()
This is also called the absolute path from the root module (root modules) My_library to the goodbye of the functional function. Third, using use
After using extern to introduce a package, you can introduce the module to the current scope with use.
extern crate My_library;
Use My_library::english::greetings::hello;
Use My_library::english::farewells::goodbye;
fn Main () {
println! (" Hello in 中文版: {} ", hello ());
println! ("Goodbye in 中文版: {}", Goodbye ());
}
In this way, you can use functional functions in the module just as you would with a local function function.
The introduction of the function function in the same module, so many modules to write repeatedly, too inconvenient.
Use My_library::english::greetings::hello;
Use My_library::english::farewells::goodbye;
This can be done with the {} shorthand:
Use My_library::english::{greetings, farewells};
Four, use pub using
In Src/english/mod.rs, two modules greetings and farewells are defined.
Pub mod greetings;
Pub mod farewells;
You can use the absolute path above to reference a functional function by using the.
My_library::english::greetings::hello ()
Modified as follows:
Pub use Self::greetings::hello;
Pub use Self::farewells::goodbye;
MoD Greetings;
MoD farewells;
Pub use introduces the functional functions in the following modules to the current scope.
Use of the time,
extern crate My_library;
Use My_library::english::hello;
Use My_library::english::goodbye;
fn Main () {
println! (" Hello in 中文版: {} ", hello ());
println! ("Goodbye in 中文版: {}", Goodbye ());
}
You can also use * to introduce all the contents of greetings into the current module scope.
Pub use self::greetings::*;
MoD Greetings;
MoD farewells;
What self is. Self refers to the current module, where self refers to the 中文版 module. In addition to self, there are super modules that point to the current module.
Self and super are relative paths. V. Use as
extern crate my_library as sayings;
Use sayings::chinese::greetings as ch_greetings;
Use sayings::chinese::farewells::*;
Use Sayings::english::{self, greetings as en_greetings, farewells as En_farewells};
fn Main () {
println! (" Hello in 中文版; {} ", En_greetings::hello ());
println! ("and in Chinese: {}", Ch_greetings::hello ());
println! ("Goodbye in 中文版: {}", English::farewells::goodbye ());
println! ("Again: {}", En_farewells::goodbye ());
println! ("and in Chinese: {}", Goodbye ());
}
The above code is well understood in addition to the fourth sentence.
The sentence is equivalent to:
Use Sayings::english;
Use sayings::english::greetings as en_greetings;
Use Sayings::english::farewells as En_farewells;