Open In App

Rust – Super and Self Keywords

Last Updated : 28 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Rust is a multi-paradigm programming language like C++ syntax that was designed for performance and safety, especially safe concurrency by using a borrow checker and ownership to validate references. The self and super keywords provided by Rust can be used in the path to remove ambiguity when accessing items like functions.

Example:

Rust




// Rust program for Super & Self Keywords
fn function_with_same_name() {
    println!("called `function_with_same_name()`
      which is in the outermost scope");
}
 
mod cool_mod {
    pub fn function_with_same_name() {
        println!("called `cool_mod::function_with_same_name()`
          which is inside the `cool_mod` module in the outermost scope");
    }
}
 
mod my_mod {
    fn function_with_same_name() {
        println!("called `my_mod::function_with_same_name()`
          which is inside the `my_mod` module");
    }
     
    mod cool_mod {
        pub fn function_with_same_name() {
            println!("called `my_mod::cool_mod::
              function_with_same_name()` which is inside the
              `cool_mod` module which is in turn inside `my_mod` module");
        }
    }
     
    pub fn indirect_call() {
        // Let's access all the functions named
        //`function_with_same_name` from this scope!
        print!("called `my_mod::indirect_call()`, that\n> ");
         
        // The `self` keyword refers to the current module
        // scope - in this case `my_mod`.
        // Calling `self::function_with_same_name()` and
        // calling `function_with_same_name()` directly both give
        // the same result, because they refer to the same function.
        self::function_with_same_name();
        function_with_same_name();
         
        // We can also use `self` to access
        // another module inside `my_mod`:
        // We have `cool_mod` module inside `my_mod`
        // module which contains `function_with_same_name`
        self::cool_mod::function_with_same_name();
         
        // The `super` keyword refers to the parent scope
        // (outside the `my_mod` module).
        super::function_with_same_name();
         
    }
}
 
fn main() {
    my_mod::indirect_call();
}


Output:

 

Explanation:

  • We start by creating a function named ‘function_with_same_name’ (we choose this name to make it easier for you to understand that we are using the same name for all the functions).
  • Then we create a module named ‘cool_mod’ which contains a public function also named ‘function_with_same_name’.
  • After that, we create another module named ‘my_mod’ which also contains a module called ‘cool_mod’ which in turn also contains a public function named ‘function_with_same_name’.
  • ‘my_mod’ also contains function named ‘function_with_same_name’  and a function named ‘indirect_call’.
  • Now we will see what is happening inside the ‘indirect_call’ function.The `self` keyword refers to the current module scope – in this case, `my_mod` which means that when we call ‘function_with_same_name()’ using self keyword it calls the ‘function_with_same_name()’ that is present in the ‘my_mod’ module.
  • After that, when we call ‘function_with_same_name()’ without using the self keyword, the output remains the same because they refer to the same function in this scope.
  • Now we call ‘function_with_same_name()’ of ‘cool_mod’ module using self keyword.
  • Now using super keyword we call ‘function_with_same_name()’ which calls the function named ‘function_with_same_name’ in the parent scope .
  • As you can see how self and super keywords make our jobs easier.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads