How to include files from same directory in a module using Cargo/Rust?

All of your top level module declarations should go in main.rs, like so:

mod mod1;
mod mod2;

fn main() {
    println!("Hello, world!");
    mod1::mod1fn();
}

You can then use crate::mod2 inside mod1:

use crate::mod2;

pub fn mod1fn() {
    println!("1");
    mod2::mod2fn();
}

I’d recommend reading the chapter on modules in the new version of the Rust book if you haven’t already – they can be a little confusing for people who are new to the language.

Leave a Comment