1

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.

hellow
  • 12,430
  • 7
  • 56
  • 79
nz_21
  • 6,140
  • 7
  • 34
  • 80
  • 1
    Does this help https://stackoverflow.com/questions/22596920/split-a-module-across-several-files ? – hellow Apr 12 '19 at 06:59
  • 3
    Please don't name a module `lib`. That could create confusion, because `lib.rs` is the root of a library crate. – starblue Apr 12 '19 at 07:55
  • 2
    By removing `pub mod lib;` you effectively remove all the code, so the error disappears. – starblue Apr 12 '19 at 07:58

1 Answers1

3

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;

Ömer Erden
  • 7,680
  • 5
  • 36
  • 45
Akiner Alkan
  • 6,145
  • 3
  • 32
  • 68