1

I have the following structure:

|-- Cargo.toml
|-- src
|   |-- file1.rs
|   |-- file2.rs
|   `-- main.rs

src/file1.rs

pub fn function1() {}

src/file2.rs

// ERROR (1): error[E0583]: file not found for module `file1`
// mod file1;

use crate::file1;
pub fn function2() {
    file1::function1();
}

src/main.rs

// ERROR (2): no `file1` in the root
// use crate::file1;
mod file1;
mod file2;

fn main() {
    file1::function1();
    file2::function2();
}

Basically, I have a different way to import function1, depending on the fact that I am in the crate root or in an arbitrary Rust file (please see ERROR (1) and ERROR (2)). I am a bit lost on how Rust manages arbirary files: they behave differently from the root crate, where a simple mod keyword does the trick. So, the mentioned answer for which this is a duplicate only partially only answers how to refer a file from the crate root, not why referring the same file from another one should be different (use crate::<filename>).

fcracker79
  • 1,118
  • 13
  • 26
  • Does this help? https://stackoverflow.com/questions/26388861/how-to-include-module-from-another-file-from-the-same-project – Peter Hall Jul 14 '19 at 09:43
  • Thanks for the answer @PeterHall. Well, that partially answers my question, as it does not include non-crate roots modules referring each other (see src/file2.rs). – fcracker79 Jul 14 '19 at 09:48
  • Place the `mod` statements in the parent module. This is how to tell Rust that the sub-modules exist at all. – Peter Hall Jul 14 '19 at 09:49
  • 1
    All paths are addressable as fully-qualified paths from the root. You can think of it as writing relative paths inside files on a file system, where the `crate` keyword is like `/`, start at the root. – Peter Hall Jul 14 '19 at 09:52
  • 2
    You never actually _need_ to use `use` statements. They are for convenience to not have to type the full paths everywhere. And they are relative to where you are in the module hierarchy. – Peter Hall Jul 14 '19 at 09:54
  • Thanks again @PeterHall. That is pretty clearer. My last doubt is: why am I using different ways of referring for `main.rs` and `file2.rs`? It must be related with the fact that one of them is the crate root but I cant't see a substantial difference. – fcracker79 Jul 14 '19 at 10:03
  • @PeterHall I am adding `pub mod f1` in `file1` and `pub mod f2` in `file2` but now I do not know how to refer them in the crate root. `use` is not accepted, neither `mod f1` does. – fcracker79 Jul 14 '19 at 10:11
  • 1
    You could write `use crate::file1;` inside main.rs too. But it would mean there would be two copies of `file1` in scope. The difference is that the modules are already in scope in main because main is the immediate parent. – Peter Hall Jul 14 '19 at 10:16
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196439/discussion-between-fcracker79-and-peter-hall). – fcracker79 Jul 14 '19 at 10:25
  • Possible duplicate of [How to include module from another file from the same project?](https://stackoverflow.com/questions/26388861/how-to-include-module-from-another-file-from-the-same-project) – hellow Jul 15 '19 at 06:25

0 Answers0