loading

Rust use Keyword

Referring to names in different modules

The entire path must be specified when calling a module’s function.

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
				
			

The entire route, a::b::c::nested_modules(), is specified when calling the nested_modules() method in the example above.

Use keyword

As we observed, there is a significant amount of function calling in the example above. Rust’s “use keyword” feature reduces the length of the function call required to include a function’s modules in the scope. Only the modules that we have indicated in the scope are brought up by the use keyword. Let’s use an example to better grasp this:

				
					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
				
			

The use keyword in the example above brings every module into the scope. Consequently, we don’t need to include the modules in the calling code in order to call the function directly.

Similar to modules, an enum is a namespace. Consequently, we may include the enum variants in the scope by using the use keyword. We can list the enum variants in the use statement by placing curly brackets around them and commas in the final position.

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
				
			

Flagcolor is the namespace whose variants are listed in the use statement in the example above. As a result, we don’t need to utilize the enum name and namespace specifier in order to use the enum variations directly.

Use of '*' operator

The * operator, also referred to as the glob operator, is used to include every item in the scope. We may eliminate the requirement to define each enum variant manually by using the glob operator.

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
				
			

The ‘*’ operator was used in the example above to include every enum variant without requiring the list to be specified in the use statement.

Use of super keyword

To go from the current module to the grandparent module, use the super keyword. It gives us access to the parent module’s private functions.

Example:

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

Output:

				
					5
				
			

The super in the module example above refers to the module’s parent module. For this reason, the private function of module an is accessible to the foo() function of module example.

Share this Doc

Rust use Keyword

Or copy link

Explore Topic