Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a module from another file from the same project?

Tags:

rust

People also ask

How to import a module in Python from another folder?

The most Pythonic way to import a module from another folder is to place an empty file named __init__.py into that folder and use the relative path with the dot notation. For example, a module in the parent folder would be imported with from .. import module .

How do you import a module in Rust?

To import the module, we use the keyword mod , followed by the file name without the . rs extension. When a module is imported, it allows us to use any code that's marked as public in the file we're importing the module from. Note that the module must be imported before we use any of its code.

What is mod rust?

Rust provides a powerful module system that can be used to hierarchically split code in logical units (modules), and manage visibility (public/private) between them. A module is a collection of items: functions, structs, traits, impl blocks, and even other modules.


You don't need the mod hello in your hello.rs file. Code in any file but the crate root (main.rs for executables, lib.rs for libraries) is automatically namespaced in a module.

To include the code from hello.rs in your main.rs, use mod hello;. It gets expanded to the code that is in hello.rs (exactly as you had before). Your file structure continues the same, and your code needs to be slightly changed:

main.rs:

mod hello;

fn main() {
    hello::print_hello();
}

hello.rs:

pub fn print_hello() {
    println!("Hello, world!");
}

If you wish to have nested modules...

Rust 2018

It's no longer required to have the file mod.rs (although it is still supported). The idiomatic alternative is to name the file the name of the module:

$ tree src
src
├── main.rs
├── my
│   ├── inaccessible.rs
│   └── nested.rs
└── my.rs

main.rs

mod my;

fn main() {
    my::function();
}

my.rs

pub mod nested; // if you need to include other modules

pub fn function() {
    println!("called `my::function()`");
}

Rust 2015

You need to put a mod.rs file inside your folder of the same name as your module. Rust by Example explains it better.

$ tree src
src
├── main.rs
└── my
    ├── inaccessible.rs
    ├── mod.rs
    └── nested.rs

main.rs

mod my;

fn main() {
    my::function();
}

mod.rs

pub mod nested; // if you need to include other modules

pub fn function() {
    println!("called `my::function()`");
}

I really like Gardener's response. I've been using the suggestion for my module declarations. Someone please chime in if there is a technical issue with this.

./src
├── main.rs
├── other_utils
│   └── other_thing.rs
└── utils
    └── thing.rs

main.rs

#[path = "utils/thing.rs"] mod thing;
#[path = "other_utils/other_thing.rs"] mod other_thing;

fn main() {
  thing::foo();
  other_thing::bar();
}

utils/thing.rs

pub fn foo() {
  println!("foo");
}

other_utils/other_thing.rs

#[path = "../utils/thing.rs"] mod thing;

pub fn bar() {
  println!("bar");
  thing::foo();
}

In a non main.rs (or lib.rs) file if you want to include from a file in the same directory then the code below works. The key is to use the word super:: for the include. (This is how I rewrite the answer of @rodo without using path)

Directory tree:

src
├── main.rs
├── my.rs
└── my
    ├── a.rs
    └── b.rs 

To include a.rs in b.rs:

src/my/a.rs:

pub fn function() {
    println!("src/my/a.rs/function()");
}

src/my/b.rs:

use super::b::function;

fn f2() {
    function();
}

src/my.rs:

mod a;
mod b;

src/main.rs:

mod my;