2

Here's my file structure:

src
├── [2.4K]  game_of_life.rs
├── [1.7K]  main.rs
└── [1.9K]  preset.rs

File contents: preset.rs:

pub struct Preset {
    ...
}

impl Preset {
    ...
}

Error I got

I tried importing Preset in main.rs:

mod preset;

And it worked fine.

After that I tried to import Preset in game_of_life.rs using the same line of code, but got an error:

Error: file not found for module `preset`
Help: name the file either game_of_life/preset.rs or game_of_life/preset/mod.rs inside the directory "src"

I don't understand why Rust is trying to find the preset in game_of_life.

What I tried to do to fix it

I found, that I have to wrap Preset in pub mod preset, but this didn't help me.

  • 4
    Hi there! I believe your question is answered by [this Q&A](https://stackoverflow.com/q/48071513/2408867). In short: the `mod` statements both go into `main.rs`. I.e. `mod preset; mod game_of_life;`. In `game_of_life.rs` you can then import `Preset` via `use crate::preset::Preset`. Remember: *first* build a module tree (no cycles!) with `mod` statements, *then* import names into scope via `use` (cycles allowed). Please let us know if this indeed answers your question, then we can mark this as duplicate. – Lukas Kalbertodt Feb 22 '20 at 09:44
  • @LukasKalbertodt, Thanks a lot! This is exactly what I needed. Thanks for explanation too. –  Feb 22 '20 at 09:48

1 Answers1

4

I found an answer: I just had to use use crate::preset::Preset. To understand how and why this works read the first comment under my question.