Including Files and Deeply Directories in Rust

One of the things that’s been criticized by newbies in Rust is the file include mechanism. About 2 days ago, I spent around 5 hours on how I was supposed to include a file that was referenced deep down in a directory tree. The docs didn’t help, as they were simple structures. Here I’ll show you how you can include your code in different parts of the application, even when the structure is complex, and save you hours on how to do this supposedly trivial task.

I’m going to refer to traits, modules, enums, functions, structs as “resources” in general.

There’s no such thing as a “file” when you reference your resource.

Rust doesn’t see files as files, but it sees them as modules and files inside folders as sub-modules. Therefore you can’t just reference them directly with a simple import or drilled namespacing a la JavaScript or C# and use them right away.

You need to create a tree of

pub mod file_name

(Called barrelling in the JS world) that expose these modules to the outer ones and (See the bullet points down below) allow them to be discovered and consumed.

How to include a module (file)

Let’s suppose we have the following structure (You can grab this from the repo).
And you’d like to include several functions from modules scattered inside the


directory into our


mod house;
// Please, do not use hyphens as this is proven to have an
// inconsistent behavior.
#[path = "./"]
mod welcome_home;
// Includes the function directly
// When using crate, you use the root directory to include everything.
use crate::house::diner;
use house::bathroom::sink::wash_face;
use house::kitchen::prepare::food_preparation::prepare_food;

fn main() {
    let user = "Jose";

 * From the docs (Link above)
 * By default, everything in Rust is private, with two exceptions:
 * Associated items in a pub Trait are public by default; Enum
 * variants in a pub enum are also public by default. When an
 * item is declared as pub, it can be thought of as being accessible
 * to the outside world.
mod building {
    pub mod lobby {
        pub(in crate) fn arrive_lobby() {
            println!("You have arrived to the lobby");
  • To include a file next to the file you just specify

    and the name of the file (without the .rs extension). E.g: mod


    . In this case welcome_home does not match the name of the file (

    Avoid using hyphens in naming everything; this was to showcase it’s possible not that it should be done), therefore we can help Rust’s compiler to identify it by providing a #path directive. Then we can reference in our code


    to call the run function directly.

  • To use a mod that exists inside the file, you can just type its name as namespace and drill down to the resource you’re looking for. Understand that if the module is not public you won’t be able to access it within your file. Check Rust’s visibility and privacy reference for more information. If you’d like to include the

    function. Since it’s in the same file you can just


    and the module is visible.

  • You can use a resource directly by specifying it with the

    keyword. This means that you don’t need to write


    to use the function, but simply



  • In case of

    which is found in


    first create a file at the root level directory or a file in /src/house/ Inside it specify the names of the folders that are included inside /src/house:

    pub mod bathroom

    . Create a

    file in


    and inside it specify the modules (file names without the .rs extension) inside



    pub mod sink; pub mod shower; pub mod toilet; 

    Note the


    modifier as this is what gives visibility outside of the speicified file. Then include them in the code via

    use house::bathroom::sink::wash_face


  • After

    , if you type


    , you’re telling the compiler to start looking from the root directory.

  • If you reference a resource via a mod and that resource is deeply nested, you only specify the first level of the module, and then drill it down. E.g: In case of

    function and enum, I only specified

    mod house

    , and then drilled down the namespaces with the double colon (::) until I reach it.

Including files or modules from another folder, directory or submodule

This is maybe the trickiest and most unnatural way for all new people to Rust to reference modules in other files. The


function is an example of a deeply nested function. I encourage you to see the repo to understand better how everything works.

Rust follows a convention: You need to create a file with the name of the directory you’d like to access, and place it next to the directory you’d like to access.

You would then need to expose each of the files inside the directory that you’d like the compiler to make it accessible:


I’d like to access the contents of


, and

inside the



First, I’d have to create a


file that would make visible or barrel, 


files. Inside it I’d specify the name of each of the files as public modules:

// Contents of /src/house/
pub mod shower;
pub mod sink;
pub mod toilet;
Then, I’d need to expose this

file from the


directory. For that, I’d need to create a


file, that would export the

file that I just defined now.

// Contents of /src/house/ or /src/
pub mod bathroom;
pub mod diner;
pub mod kitchen;
As you can see

pub mod bathroom


file visible. Note the convention:

pub mod file_name_without_rs_extension

You need to do this everytime you’d like to make a module (file) visible to the outer module (directory/folder).

Note: You’d see in the repo, and in the picture above, that there are

files; In versions before Rust 2018, the only way for Rust to discover these directories was for them to specify

inside the directory.

This became problematic because if you wanted to specify a file with the same name of the directory, the compiler would get confused. That’s why starting Rust 2018 (Look for the line which says

edition = "2018"

in your



In this example


is the same as


You can create any of those files and it would work.

Including other sibling modules (files) within submodules

To include sibling files within submodules, such as the /src/house/


within the


file you can get to by using the


keyword in your



pub mod dessert;
pub mod main_dish;
use self::main_dish::lasagna;
// use super::diner::main_dish::lasagna;

pub fn eat() {
    let candy = dessert::candy::Candy::new_chocolate(10);
For example, the code below uses self to navigate relatively to the current module’s location, and then drill it to the



You can also use


and that will start from the parent module.

Some things you should know:


  • Rust 2018 – Modules, External Crates, Submodules, Paths and Visibility Modifiers by TensorProgramming – (Best resource that explains how to include everything)
  • Including crates with hyphen vs underscore debate:

read original article here