Skip to content

Latest commit

 

History

History

mod-keyword-for-nested-hierarchies

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

mod keyword for nested hierarchies

The Rust mod keyword can provide nested hierarchies, meaning that a modules can contain other modules:

pub mod outer {
    pub mod inner {
        pub fn hello() {
            println!("Hello");
        }
    }
}

fn main() {
    outer::inner::hello()
}

You can optionally add a use statement such as:

use outer::inner::hello;
fn main() {
    hello()
}

Module hierarchies can help test-driven development, because you can create an outer module tests, with an inner module for each function, to improve readability and encapsultation:

#[cfg(test)]
mod tests {
    mod my_function_1 {
        #[test]
        fn test_something() {
            assert!(/* ... */);
        }
    }
    /* ... */
}