nz_21
nz_21

Reputation: 7343

Confused about rust modules

I have three files in src/, like so:

lib.rs

pub mod first

first.rs

fn hello() {}

main.rs

pub mod lib

This gives me an error saying:

error[E0583]: file not found for module `first`
 --> src/lib.rs:1:9
  |
1 | pub mod first;
  |         ^^^^^
  |
  = help: name the file either lib/first.rs or lib/first/mod.rs inside the directory "src"

Now, if I remove pub mod lib from main.rs, everything compiles fine.

I don't understand why this is happening.

Upvotes: 1

Views: 817

Answers (1)

Akiner Alkan
Akiner Alkan

Reputation: 6862

The help that compiler says is very meaningful. When you write pub mod first; inside of a lib.rs it checks for the first.rs file or first folder inside a lib folder and a mod.rs file.

Please note that mod.rs usages are changed with Rust 2018. Reference

Now, if I remove pub mod lib from main.rs, everything compiles fine.

When you remove pub mod lib; from your main,

You basically say that this code will not be used in production therefore it is not needed to compile even. So basically the code will not included to compile.

This is why it works when you remove the pub mod lib;

Upvotes: 3

Related Questions