Javatpoint Logo
Javatpoint Logo

Referring to names in different modules

When we call the function of a module, then we need to specify the full path.

Let's understand this concept through an example:

Output:

Nested Modules

In the above example, nested_modules() function is called by specifying the full path, i.e., a::b::c::nested_modules().

use keyword

In the above scenario, we saw that the function calling is quite lengthy. Rust "use keyword" shortens the length of the function calling to bring the modules of a function in the scope. The use keyword brings only those modules which we have specified in the scope. Let's understand this through an example:

Output:

Nested Modules

In the above example, the use keyword includes all the modules into the scope. Therefore, we can call the function directly without including the modules in the calling function.

An enum is also a form of a namespace like modules. Therefore, we can use the use keyword to bring the enum variants into the scope. In use statement, we can list the enum variants in the curly brackets and the commas in the last position.

Let's understand through an example:

Output:

orange
white
green

In the above example, Flagcolor is the namespace whose variants are specified in the use statement. Therefore, we can directly use the enum variants without using enum name and namespace specifier.

Use of '*' operator

The * operator is used to bring all the items into the scope, and this is also known as glob operator. If we use the glob operator, then we do not need to specify the enum variants individually.

Let's understand this through an example:

Output:

Red
Yellow
Green
Orange

In the above example, the '*' operator has been used to include all the enum variants without specifying the list in the use statement.

Use of super keyword

The super keyword is used to access the grandparent module from the current module. It enables us to access the private functions of the parent module.

Output:

5

In the above example, the module example has used the super which refers its parent module. Due to this reason, foo() function of module example can access the private function of module a.


Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials