Skip to content

Latest commit

 

History

History

mod-keyword-for-module-namespaces

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

mod keyword for module namespaces

In Rust, namespaces are a way to organize and group related items, such as functions, types, and constants, under a common name. Namespaces are implemented using modules, which are Rust's primary mechanism for organizing code into reusable components.

Modules can be defined using the mod keyword, followed by the name of the module and its contents enclosed in curly braces:

mod my_module {
    fn private_function() {
        // implementation details here
    }
    pub fn public_function() {
        // implementation details here
    }
}

In this example, my_module is a module that contains two functions: private_function, which is not visible outside of the module, and public_function, which is marked as pub and can be accessed from other modules.

To use a module from another module, you can use the use keyword to bring its contents into scope:

use my_module::public_function;

fn main() {
    public_function();
}

In this example, we bring the public_function from my_module into the scope of main, allowing us to call it directly.

Overall, namespaces in Rust provide a powerful mechanism for organizing and structuring code, enabling developers to write more modular, reusable, and maintainable software.