Industrial Training




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:
 pub mod a  
{  
  pub mod b  
  {  
    pub mod c  
    {  
      pub fn nested_modules()  
      {  
        println!("Nested Modules");  
      }  
    }  
  }  
 }  
  
fn main()  
{  
 a::b::c::nested_modules();  
}  

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:


 pub mod a  
{  
  pub mod b  
  {  
    pub mod c  
    {  
      pub fn nested_modules()  
      {  
        println!("Nested Modules");  
      }  
    }  
  }  
 }  
  
use a::b::c::nested_modules;  
fn main()  
{  
  nested_modules();  
}  

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:
 #[derive(Debug)]  
enum Flagcolor  
{  
 Orange,  
 White,  
 Green,  
}  
use Flagcolor::{Orange,White,Green};  
fn main()  
{  
  let _o= Orange;  
  let _w= White;  
 let _g= Green;  
 println!("{:?}",_o);  
println!("{:?}",_w);  
println!("{:?}",_g);  
}  

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:
 #[derive(Debug)]  
enum Color  
{  
  Red,  
  Yellow,  
  Green,  
  Orange,  
}  
  
use Color::*;  
fn main()  
{  
  let _red=Red;  
  let _yellow=Yellow;  
  let _green=Green;  
  let _orange=Orange;  
  println!("{:?}",_red);  
  println!("{:?}",_yellow);   
  println!("{:?}",_green);  
  println!("{:?}",_orange);  
}  

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.


mod a{  
fn x() -> u8 {  
    5  
}  
  
pub mod example {  
    use super::x;  
  
    pub fn foo() {  
        println!("{}",x());  
    }  
}}  
  
fn main()  
{  
  a::example::foo();  
}  

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.




Hi I am Pluto.