12

Rust cannot find local file through declaration. I have the following file structure in src:

| main.rs
| lib.rs
| test.rs
| prog.rs
| file.rs

In main.rs I have

extern crate cratename;
mod prog;

use cratename::file1::File1;
...
prog::function() // This works
...
#[cfg(test)]
mod test;

In lib.rs I have

pub mod file1;

In test.rs I have

extern crate cratename
use cratename::file1::File1;
mod prog;
#[test]
...
prog::function() // This does not work
...

I keep getting the error

error[E0583]: file not found for module `prog`

I tried putting the declaration in lib.rs but that didn't work. I've tried numerous other permutations involving this declaration along with main.rs but none of it worked. If it helps, prog.rs doesn't contain a struct and it doesn't use the mod keyword, it only contains public functions. This has stumped me for a while. Thank you for your help.

mr_broccoli
  • 5,759
  • 4
  • 9
  • 9
  • 5
    `mod prog;` in test.rs declares a module, `test::prog`, that is distinct from `prog`. main.rs and lib.rs are special in this respect. If you want to use a module that is defined elsewhere, you use `use` rather than `mod`. – trent Aug 21 '19 at 13:37
  • @trentcl That worked, thank you so much – mr_broccoli Aug 21 '19 at 13:42
  • 2
    Be sure to re-read [the book chapter on this](https://doc.rust-lang.org/book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html). – Shepmaster Aug 21 '19 at 13:43

0 Answers0