Rust confused file system

I read tons of document but I can't understand this file/module system of Rust. I'm trying to seperate modules but I can't. Because everytime I'm getting error and says:

  • can't find crate
  • file not found for module modules
  • unresolved import crate::modules
  • file not found for module test1
  • unresolved import modules
  • failed to resolve: there are too many initial supers.
  • unused import: modules::mod

etc.

I have a project that name is "Project1". And this project contains two folder: Src(this folder contains main.rs as you know) and Modules (I created this)
Src folder contains main.rs and Modules folder contains mod.rs, test1.rs, test2.rs.

Now I'm asking you. How I include this "modules" file in the main.rs ? Is this file system really hard to understand or am I only who don't understand this ?

I tried these:

  • use modules
  • mod modules
  • extern crate modules
  • use crate::modules
  • use super::modules
  • use self::modules
  • mod modules/test1
  • mod modules/test1.rs
  • mod test1.rs
  • mod test1

If you have not read this yet, you may want to read it first. If you have already read it and not understand something, ask about the part that you not understand here!

1 Like

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

Sorry if my words is not good (English is not my native). But that chapter is written for modules/crate in Rust. If I say something, I just repeat information from it. If you have a code a repo somewhere, then people can point out where is the problem for you.

1 Like

For example, you can have a repo like this. Then, someone may come up with a help like this.

1 Like

Here's another good blog post on how the module system relates to the file system: https://fasterthanli.me/blog/2019/rust-modules-vs-files/

3 Likes

Based on what you described you should have:

// lib.rs
pub mod modules;

// modules/mod.rs
pub mod test1;
pub mod test2;

Now you can import things from modules using use crate::modules::foo and stuff from test1 using use crate::modules::test1::bar.

Note that you wrote your modules folder in upper case, but it should be lower case.

What is unusual in your setup is that "modules" directory is alongside "src" directory, instead of being inside it. This is the reason why all your attempts failed.

This is solvable by adding #[path = "../modules/mod.rs"] attribute before referencing "modules" module in main.rs.

If I understand correctly your setup is the following:


├── modules
│   ├── mod.rs
│   ├── test1.rs
│   └── test2.rs
└── src
    └── main.rs

For example, following file contents:

modules/mod.rs

mod test1;
mod test2;

pub use test1::f1;
pub use test2::f2;

modules/test1.rs

pub fn f1() {
    println!("I am in test1 module!")
}

modules/test2.rs

pub fn f2() {
    println!("I am in test2 module!")
}

src/main.rs

#[path = "../modules/mod.rs"]
mod modules;
use modules::{f1, f2};

fn main() {
    f1();
    f2();
}

display:

I am in test1 module!
I am in test2 module!