Reputation: 1603
I have the following project structure:
src
├── module1
│ └── mod.rs
├── main.rs
└── module2
└── mod.rs
but I get a
error[E0432]: unresolved import `crate::module2`
--> src/module2/mod.rs:6:14
|
6 | use crate::module2::SomeStruct;
| ^^^^^^^ maybe a missing crate `module2`?
When the contents of the files as as follows.
pub mod module1 {
// -- snip --
use crate::module2::SomeStruct;
}
// -- snip --
}
pub mod module2 {
// --snip--
pub struct SomeStruct;
}
// -- snip--
}
mod module1;
fn main() {
// -- snip--
}
Why is this and how can it be fixed? All relevant modules and structs are public. A relevant chapter in the Rust Book.
Upvotes: 3
Views: 942
Reputation: 10474
The declaration of a module (e.g. pub mod module1
) happens outside of that module. There are two kinds of module declarations: one where the definition is right after (inside braces), and one where the definition is in another file.
For modules in a separate file, you'll simply say pub mod module1;
in that module's parent. For your structure, you'll want to have pub mod module1;
and pub mod module2
in main.rs
.
Inside the module file (e.g. src/module1/mod.rs
), you don't need pub mod module1
at all. You can just have its items directly in the file.
So your setup should be
src/main.rs
pub mod module1;
pub mod module2;
src/module1/mod.rs
// -- snip --
use crate::module2::SomeStruct;
// -- snip
src/module2/mod.rs
// --snip--
pub struct SomeStruct;
// -- snip--
Upvotes: 2